Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 1.46 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package zwei;
  2.  
  3. import java.util.HashMap;
  4.  
  5.  
  6.  
  7. public class bla {
  8.  
  9.         //Shallow copy
  10.         static HashMap<Integer, String> kopie = new HashMap<Integer, String>();
  11.         //deep Copy
  12.         static HashMap<Integer, String> kopie2 = new HashMap<Integer, String>();
  13.         //ursprüngliche map zum verändern
  14.         static HashMap<Integer, String> zwo = new HashMap<Integer, String>();
  15.  
  16.         //Shallow copy constructor
  17.         public bla(HashMap<Integer, String> zwo){
  18.                 kopie = zwo;
  19.         }
  20.         //Deep copy constructor (int i ist nur zum aufrufen wichtig!)
  21.         public bla(int i, HashMap<Integer, String> zwo){
  22.                 for(Integer single : zwo.keySet()){
  23.                         kopie2.put(single, zwo.get(single));
  24.                 }
  25.         }
  26.        
  27.        
  28.         public static void main(String[] args) {
  29.                 //Map wird mir inhalt gefüllt
  30.                 for(int i=0 ; i<10 ; i++){
  31.                         zwo.put(i, "wurstkopf"+i);
  32.                 }
  33.  
  34.                 //Shallo copy wird erzeugt
  35.                 bla abc = new bla(zwo);
  36.                 //Deep copy wird erzeugt
  37.                 bla def = new bla(1, zwo);
  38.                
  39.                 //Ursprüngliche map wird verändert um shallo copy zu beweisen
  40.                 zwo.remove(2);
  41.                 zwo.put(2, "Neeee");
  42.                 zwo.remove(6);
  43.                 zwo.put(6, "BÄM");
  44.                
  45.                
  46.                 //ausgabe kopie (veränderungen müssen auftauchen)
  47.                 for(Integer single : kopie.keySet()){          
  48.                         System.out.println(single);
  49.                         System.out.println(kopie.get(single));
  50.                        
  51.                 }
  52.                
  53.                 System.out.println("-----------");
  54.                 //ausgabe kopie (ohne veränderungen!)
  55.                 for(Integer single : kopie2.keySet()){         
  56.                         System.out.println(single);
  57.                         System.out.println(kopie2.get(single));
  58.                        
  59.                 }
  60.  
  61.         }
  62.  
  63. }