Java Collection comparison ---- collection (continued), java ---- Collection

Source: Internet
Author: User

Java Collection comparison ---- collection (continued), java ---- Collection

Set Interface

The Set interface is rarely mentioned in class, so there are not many understandings of it. The elements in the class that implement the set interface are not repeated. It depends on map. In fact, the saved value is the saved key. In map, the key will not be repeated, so there will be no repeated elements in set. Its implementation class has three: hashSet, TreeSet, LinkedHashSet.

1 public static void main (String [] args) {2 // Set <Integer> set = new HashSet <Integer> (); 3 // Set <Integer> set = new TreeSet <Integer> (); 4 Set <Integer> set = new LinkedHashSet <Integer> (); 5 set. add (null); 6 Random rd = new Random (); 7 System. out. println ("add order:"); 8 for (int I = 0; I <20; I ++) {9 int x = rd. nextInt (30); 10 set. add (x); 11 System. out. print (x + ","); 12} 13 System. out. println (); 14 15 System. out. println ("traversal order:"); 16 Iterator iterator = set. iterator (); 17 while (iterator. hasNext () {18 System. out. print (iterator. next () + ","); 19} 20}

 

 

HashSet: It is unordered and can save null. The sequence and traversal result are as follows:

LinkedHashSet: It maintains the order of adding elements and can save null. The order of adding and traversing results are as follows:

TreeSet: It is an ordered set. elements are arranged in order, but null cannot be saved. The order of adding and traversing results are as follows:

1 public class Model implements Comparable {2 String str; 3 int num; 4 @ Override 5 public int compareTo (Object arg0) {6 Model tmp = (Model) arg0; 7 int cmp = this. num-tmp. num; 8 if (cmp = 0) {9 return str. compareTo (tmp. str); 10} 11 return cmp; 12} 13} 14 15 public static void main (String [] args) {16 TreeSet <Model> ts = new TreeSet <Model> (); 17 Model m2 = new Model (); 18 m2.str = "121"; 19 m2.num = 1; 20 ts. add (m2); 21 22 Model m3 = new Model (); 23 m3.str = "121"; 24 m3.num = 2; 25 ts. add (m3); 26 27 Model m1 = new Model (); 28 m1.str = "321"; 29 m1.num = 1; 30 ts. add (m1); 31 32 Iterator <Model> it = ts. iterator (); 33 while (it. hasNext () {34 Model tmp = it. next (); 35 System. out. println (tmp. str + ":" + tmp. num); 36} 37}

 

Related Article

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.