Set is a non-repeatable collection interface, set retrieval efficiency is relatively low, compared with the list, insert and delete more efficient, will not cause the change of other elements position, set is unordered, set and list the biggest difference is that set does not allow repetition
We have to remember that this guy can't repeat .
The reason that set does not allow duplication is that he is based on a map, and the key in the map is not allowed to be duplicated, only the key in the map is used in the set
Set common methods, please refer to the Java language API for more information
add(E e): Add this element if the specified element is not already present in the set
addAll(Collection<? extendsE> c): If all elements in collection are not specified in the set, add them to this set
clear(): Removes all elements from this set
contains(Object o): Returns true if the set contains the specified element
containsAll(Collection<?> c): Returns true if this set contains all the elements of the specified collection
equals(Object o): Compares the equality of the specified object to this set
isEmpty(): Returns true if the set does not contain an element
iterator(): Returns an iterator that iterates over the elements in this set
remove(Object o): If the specified element exists in the set, it is removed
removeAll(Collection<?> c): Removes the elements in the set that are contained in the specified collection
retainAll(Collection<?> c): Preserves only those elements in the set that are contained in the specified collection
size(): Returns the number of elements in a set
toArray(): Returns an array containing all the elements in the set
Compared to the egg, the set has no get method to get the element object, only through the iterator to access the data
Our common set implementation classes are: TreeSet, HashSet, and Linkedhashset
TreeSet TreeSet Incredibly orderly, the default is the natural sequence, the sort can be used by the use of, or you can use the custom, when creating TreeSet, pass a custom collation object, and since it is orderly, there is based on TREEMAP implementation, Then the element object in the TreeSet must implement the comparable interface, but if it is a string object, hehe, no need, the string itself has implemented the comparable interface. case: to be supplemented HashSet
As with TreeSet, data can only be accessed through iterators, HashSet is based on HASHMAP implementations, but is unordered, common methods are similar to set interfaces, remember that access must pass through iterators, the order of iterations is not guaranteed to be the same as the order of insertion, This is exactly the opposite of Linkedhashset.
Case: To be supplemented
Linkedhashset
Linkedhashset is a list structure that is consistent with the above, traversed by iterators, but with the same order of traversal and insertion.