Collection (3) - Set

--- --- restore content begins

First, what is Set?

Set interface is a sub-interface Collection interface, is a duplicate set of elements is not allowed.

 

Two, HashSet

  • HashSet is a typical implementation class Set disorderly, can not be repeated, allowed to join NULL, most of the time using the Set is the use of this class, HashSet in accordance with Hash algorithm storage elements, hence, have very good access and lookup performance.
  • The underlying fact is an array, the meaning of existence is to speed up queries, we know there is no relationship between the value and position of the general array elements in index positions which are random, elements, therefore, to find in the array when the specific elements can only compare apples to apples, look for the number of speed depends on the comparison. However, the underlying array HashSet, there is determined the relationship between the index value and the: index = hash (value), it is possible to quickly find the elements or indexed by this relationship.
  • When the HashSet storage elements, first calls the object hashCode () method, obtaining hashCode value, according to this value, find the corresponding position, the HashSet allowed to repeat, it is determined: first Compare hashCode are the same except that it is the different objects, then compare it equals the same method, according to the result of the determination.
  • Each memory to hash table object, must offer equals and hashCode methods implemented to determine whether the object is the same as for HashSet, we should ensure that returns true if the two objects by the equals method, hashCode values ​​are the same.

 

 

Three, LinkedHashSet

LinkedHashSet are ordered, not allowed to repeat, allowed to join the collection of NULL.

It is also determined value to memory location HashCode element, also using linked list to maintain the order of the elements, which makes it ordered according to the order of insertion. Compared Hashset multi-maintained list, so performance is slightly lower than HashSet.

 

Four, TreeSet

TreeSet is ordered, not allowed to repeat, does not allow NULL collection. Underlying the use of red-black tree algorithm, good range queries.

  • If no arguments TreeSet constructor creates a new TreeSet object claim wherein the storage elements implemented Comparable interface, where it can not be deposited NULL
  • Must deposit the same type of objects (default sort), otherwise there will be type cast exceptions! The general use of generics to be limited.
  • Natural ordering: parameter object required to be added must implement Comparable interface, and covers compareTo (Object obj) Method:

  If this> obj, returns 1,

  If this = obj, returns 0,

  If this <obj, returns -1.

  After comparison rules defined by this method, in ascending order.

  • Custom ordering: When creating TreeSet object class Compartor passed realized its interface, defined by the compare rules customized sorting method (e.g., reverse), compare the requirements of the method and the equals method consistent implementation class, namely: compare method in two equal objects should return 0.

As follows, in descending order according to how much money:

 1 package com.vi.collection.set;
 2 
 3 import java.util.Comparator;
 4 import java.util.TreeSet;
 5 
 6 public class TreeSetDemo {
 7     public static void main(String[] args) {
 8         Man m1 = new Man(1000);
 9         Man m2 = new Man(2000);
10         Man m3 = new Man(3000);
11         Man m4 = new Man(2500);
12         TreeSet<Man> ts = new TreeSet<>(new Man(0));
13         ts.add(m1);
14         ts.add(m2);
15         ts.add(m3);
16         ts.add(m4);
17         System.out.println(ts);
18     }
19 }
20 
21 class Man implements Comparator<Man> {
22     double money;
23 
24     public int compare(Man m1, Man m2) {
25         if (m2.money > m1.money)
26             return 1;
27         if (m2.money == m1.money)
28             return 0;
29         return -1;
30     }
31 
32     public Man(double money) {
33         this.money = money;
34     }
35 
36     public String toString(){
37         return money+" ";
38     }
39 
40 }
View Code

 

 

 

 

 Fifth, to compare the above three Set

Common:

1. duplicate values ​​are not allowed into

2. The threads are unsafe (solution: use Collections.synchronizedSet () method)

 

difference:

1.TreeSet not allow NULL values, HashSet and LinkedList can be stored in null

2.HashSet the underlying disorder algorithm uses a hash table, high search efficiency by equals () and hashCode () determines whether the common objects are equal, i.e., to deposit an object override equals () and hashCode () consistent ;

LinkedList underlying structure using the hash table and the linked list, adding both to ensure the order of the elements, but also to ensure the efficiency of the query, HashSet slightly lower performance;

TreeSet does not guarantee the order of addition of the elements, but the elements in the collection will be sorted according to the sort of natural or custom rules, are based on a red-black tree algorithm (tree structure is suitable for a range query).

 

Guess you like

Origin www.cnblogs.com/blogforvi/p/11626793.html