"Turn" hashmap shallow copy and deep copy--good

Source: Internet
Author: User

Original URL: http://canofy.iteye.com/blog/258790

Java deep copy and shallow copy
  1. Shallow copy and deep copy
  2. //Shallow copy: All variables of the copied object contain the same value as the original object, and all references to other objects still point to the original object.
  3. //In other words, shallow copy simply duplicates the object being considered, not the object it refers to.
  4. //Deep copy: All the variables of the copied object contain the same values as the original object, removing those variables that refer to other objects.
  5. The variables that refer to other objects will point to new objects that have been copied, not those that are already referenced.
  6. //In other words, deep copy copies the objects that are referenced by the object being copied again.
  7. ///1, Direct Assignment (outside the string is a shallow copy)
  8. //2, using the constructor (deep copy)
  9. //3, using the Clone () method (deep copy)
  10. //String (not understood without Colne () method)
  11. String s="SSS";
  12. String T=s; //Deep copy
  13. String y=new string (s); //Deep copy
  14. System.out.println ("s:" +s+"T:" +t+"y:" +y ");
  15. t="TTT";
  16. y="yyy";
  17. System.out.println ("s:" +s+"T:" +t+"y:" +y ");
  18. //Array
  19. String[] ss={"111","222","333"};
  20. String[] TT=SS; //Shallow copy
  21. String[] ww= (string[]) ss.clone (); //Deep copy
  22. System.out.println ("ss:" +ss[1]+"tt:" +tt[1]+"ww:" +ww[1]);
  23. tt[1]="2t2";
  24. ww[1]="2w2";
  25. System.out.println ("ss:" +ss[1]+"tt:" +tt[1]+"ww:" +ww[1]);
  26. //list List
  27. ArrayList a=New ArrayList ();
  28. For (int i=0;i<10;i++) {
  29. A.add (string.valueof (i+1));
  30. }
  31. ArrayList B=a; //Shallow copy
  32. ArrayList c=New ArrayList (a); Deep copy
  33. ArrayList d= (ArrayList) A.clone (); //Deep copy
  34. System.out.println ("A:" +a.get (1) +"B:" +b.get (1) +"C:" +c.get (1) +"D:" +d.get (1));
  35. B.set (1, "BBB");
  36. C.set (1, "CCC");
  37. System.out.println ("A:" +a.get (1) +"B:" +b.get (1) +"C:" +c.get (1) +"D:" +d.get (1));
  38. //hashmap
  39. HashMap h=New HashMap ();
  40. H.put ("1", "HHH");
  41. HashMap m=h; //Shallow copy
  42. HashMap p=New HashMap (h); Deep copy
  43. HashMap n= (HASHMAP) H.clone (); //Deep copy
  44. System.out.println ("H:" +h.get ("1") +"M:" +m.get ("1") +"P:" +p.get ("1") + "N:" +n.get (  "1"));
  45. M.put ("1", "MMM");
  46. P.put ("1","PPP");
  47. N.put ("1", "nnn");
  48. System.out.println ("H:" +h.get ("1") +"M:" +m.get ("1") +"P:" +p.get ("1") + "N:" +n.get (  "1"));

"Turn" hashmap shallow copy and deep copy--good

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.