Java Collection comparison----Collection (cont.)

Source: Internet
Author: User

Set interface

About the set interface, is seldom mentioned in class, so there is not much understanding of it, the implementation of the Set interface class, the elements are not duplicated . He relies on the map, in fact, we save the value is a saved key, in the map, the key will not repeat, so set there is no duplicate element, its implementation class has three: Hashset,treeset,linkedhashset.

1  Public Static voidMain (string[] args) {2         //set<integer> Set = new hashset<integer> ();3         //set<integer> Set = new treeset<integer> ();4Set<integer> set =NewLinkedhashset<integer>();5Set.add (NULL);6Random rd =NewRandom ();7System.out.println ("Add Order:");8          for(inti = 0; I < 20; i++) {9             intx = Rd.nextint (30);Ten Set.add (x); OneSystem.out.print (x + ","); A         } - System.out.println (); -  theSYSTEM.OUT.PRINTLN ("Traversal Order:"); -Iterator Iterator =set.iterator (); -          while(Iterator.hasnext ()) { -System.out.print (Iterator.next () + ","); +         } -}

HashSet: He is unordered, can save null, add order and traverse results as follows:

Linkedhashset: He maintains the order in which elements are added, can save null, add order and traverse results as follows:

TreeSet: He is an ordered set of elements in order, but cannot save null, add order and traverse results as follows:

We define our own classes, if we want to join the TreeSet, we need to implement the CompareTo method in interface comparable, define your own comparison rules, or throw an exception.

To a demo, the custom rule is to compare int, then compare string, no matter how the order of MODEL1,MODEL2,MODEL3 is changed, the output is the same, eg:

1  Public classModelImplementsComparable {2 String str;3     intnum;4 @Override5      Public intcompareTo (Object arg0) {6Model tmp =(Model) arg0;7         intCMP = This. Num-Tmp.num;8         if(CMP = = 0){9             returnStr.compareto (TMP.STR);Ten         } One         returnCMP; A     } - } -  the  Public Static voidMain (string[] args) { -treeset<model> ts =NewTreeset<model>(); -Model m2 =NewModel (); -M2.str = "121"; +M2.num = 1; - Ts.add (m2); +          AModel m3 =NewModel (); atM3.str = "121"; -M3.num = 2; - Ts.add (m3); -          -Model M1 =NewModel (); -M1.STR = "321"; inM1.num = 1; - Ts.add (M1); to          +Iterator<model> it =ts.iterator (); -          while(It.hasnext ()) { theModel tmp =It.next (); *System.out.println (Tmp.str + ":" +tmp.num); $         }Panax Notoginseng}

Java Collection comparison----Collection (cont.)

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.