Set collection interfaces

Set interface and the interface List biggest difference is that the content Set interface is not allowed to repeat the same time, the interface Set interface does not approach the expansion of the Collection, and the List interface Collection has been expanded, therefore, Set interface does not get ()method

Set interface include: HashSet (memory disorder) and TreeSet (orderly storage)

(1)HashSet 

Features:

 not guarantee the order of the elements, the order may change
 not synchronized
 collection elements may be null, but only into a null

Adding elements

public class TestHashSet {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();  //<>泛型,可以使任何数据类型
        Set<Integer> set1 = new HashSet<>(); //但是要包装类,这里不能是int型
        set.add("A");
        set.add("B");
        set.add("C"); //重复元素,被后一个覆盖,不在结果中
        set.add("C"); 
        set1.add(1);
        set1.add(2);
        set1.add(2); //重复元素
        set1.add(3);
        set1.add(4);
        System.out.println(set);
        System.out.println(set1);
    }
}

result:

[A, B, C]
[1, 2, 3, 4]

Process finished with exit code 0

 You might want to see here confused, HashSet not disorderly storage Well, how output is ordered ??? emmm ...... listen to me slowly

When the HashSet into a collection element, the object is invoked HashSet hashCode () method to get the value of the object hashCode, and then determines the object storage locations in the HashSet The hashCode value here is only an algorithm Hash Code coincidence; gives the illusion of an orderly in a small area;

[A, B, C]
[8080, 1024, 1, 2, 3, 6, 10]

Process finished with exit code 0

When I insert a few big numbers when the output becomes a disorderly

to sum up:

(1) When the value of the size about the same size of the input and the underlying array, HashSet input sequence is disordered, but the output is ordered, such as the number of hash values, the order is taken at the bottom, so the output is Ordered

(2) When the value of the underlying array size and input of difference is large, a large number of possible hash value is smaller than the number of younger than him, and this time the result is output disorderly.

(2)TreeSet

TreeSet SortedSet is the only class that implements the interface, to ensure that the collection element is TreeSet ordered state. TreeSet supports two sort of natural sorting and custom ordering in which the natural order is the default sort order. TreeSet to join in should be the object of the same class.

Use natural ordering of the elements to be sorted CompareTo (Object obj) method to compare the magnitude relationship between elements, and the elements are arranged in ascending order.

Adding elements

public class TestTreeSet {
    public static void main(String[] args) {
        Set<String> set = new TreeSet<>();
        Set<Integer> set1 = new TreeSet<>();
        set.add("C");
        set.add("C");  //重复元素
        set.add("A");
        set.add("B");
        set1.add(8080);
        set1.add(6);
        set1.add(1024);
        set1.add(3);
        set1.add(10);
        set1.add(1);
        set1.add(2);
        set1.add(2);  //重复元素
        System.out.println(set);
        System.out.println(set1);
    }
}

result:

[A, B, C]
[1, 2, 3, 6, 10, 1024, 8080]

Process finished with exit code 0

Found from the output result of the above TreeSet disorder is outputted into order of HashSet, TreeSet be output in ascending order 

(3) repeat element determines 

When using the data stored TreeSet subclasses determines repeating elements rely ComParable interfaces completed. Equal returns 0, 0 is returned as long as, on behalf of repeat elements

Comparable HashSet but with nothing to do, so it is repeated to determine the elements of an Object method relies on two classes:

    1. hash code: public native int hashCode ();
    2. Comparison of the object: public boolean equals (Object obj) ;

That is to say if it is to identify the object's unique, necessarily equals () and hashCode () method call common. Analyzing HashSet set equal to two standard elements equals method by comparing two objects are equal, and the two objects hashCode () method returns a value equal to

Note: If the object is put into a HashSet, the rewriting equals method corresponding to the object class, which should override hashCode () methods. Which is the rule returns true if the two objects by comparing the equals method, which should be the same hashCode. Further, as the object attribute equals relatively standard, it should be used to calculate the value of hashCode. 

(4) HashSet and TreeSet differences and relations

contact:

  • Set all sub-interfaces
  •  Singleton set
  • Data not require repeated

the difference:

  • HashSet hash table is implemented, disorderly data when allowed into the null value, but only into a null
  • TreeSet a binary tree, the data is automatically sorted, and ascending, not allowed to put a null value
  • Hashset by replication hashCode () method and equals () methods to ensure the only way to store data
  • Treeset by compareto Compareable method for storing data interface to ensure that the only way
  • HashSet requirements must implement objects into HashCode () method, into object code as is a hashcode identification, the String object with the same content, the same hashcode, so that the contents can not be repeated placed. However, with a class of objects can be placed in different instances.

 

Guess you like

Origin blog.csdn.net/weixin_43224539/article/details/91374358