1. overview
Set interface is a sub-interface of Collection and IterAble. The elements in List collection are not sequential and can not be repeated. The container classes that implement the List interface are HashSet < E > class and TreeSet < E > class.
HashSet class, does not allow duplicate elements; allows elements with null values, but only one at most; does not guarantee the order of elements in the collection, HashSet in the addition of elements, will call the hashCode() method of elements, get the hash code of the element, according to this hash code to calculate the storage location of elements in the collection, HashSet search efficiency is relatively high, and its storage. The way is concerned; the HashSet class is not synchronous.
The add() method of the HashSet class implements the following:
public boolean add(E e) { return map.put(e, PRESENT)==null; }
map and PRESENT are member variables of the HashSet class.
private transient HashMap<E,Object> map; // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object();
The TreeSet class inherits the SortedSet < E > interface. The elements in the TreeSet set are sorted in ascending order according to the size of the elements; the TreeSet class is not synchronous.
The add() method of the TreeSet class implements the following:
public boolean add(E e) { return m.put(e, PRESENT)==null; }
m and PRESENT are member variables of the HashSet class.
private transient NavigableMap<E,Object> m; // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object();
Navigable Map < K, V > interface inherits SortedMap < K, V > interface.
public interface NavigableMap<K,V> extends SortedMap<K,V>
2. Specific use of HashSet classes
Some of the methods of HashSet are as follows. Visit all of them. JDK8 or JDK9.
Modifiers and types | Method | describe |
boolean | add(E e) | If the specified element does not yet exist, it is added to this collection. |
void | clear() | Delete all elements in this collection. |
Object | clone() | Returns a shallow copy of this HashSet instance. Learn from shallow and deep copies of Java, Click to go |
boolean | contains(Object o) | If this set contains the specified element, return true or false. |
boolean | isEmpty() | If this set does not contain any elements, return true or false. |
Iterator<E> | iterator() | Returns an iterator for the element in this set. |
boolean | remove(Object o) | If it exists, the specified element is removed from the collection. |
int | size() | Returns the total number of elements in this collection. |
The test code is as follows:
package set; import java.util.HashSet; import java.util.Iterator; public class MySetStudy{ public static void main(String args[]) { HashSet<String> hashSet = new HashSet<String>(); hashSet.add("1"); hashSet.add("3"); hashSet.add("3"); hashSet.add("2"); hashSet.add("12"); hashSet.add("6"); Iterator iterator = hashSet.iterator(); System.out.print("hashSet Numerical value: "); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } System.out.print("\nhashSet Of hashCode value: "); iterator = hashSet.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next().hashCode() + " "); } HashSet hashSet1 = (HashSet) hashSet.clone(); iterator = hashSet1.iterator(); System.out.print("\nhashSet1 Numerical value: "); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } } }
Running screenshots are shown below. According to the results, HashSet is out of order, nor is it sorted in ascending order according to hashCode value.
3. Specific use of the TreeSet class
Some of TreeSet's methods are shown below, all of which are accessible JDK8 or JDK9.
Modifiers and types | Method | describe |
boolean | add(E e) | If the specified element does not yet exist, it is added to this collection. |
Object | clone() | Returns a shallow copy of this TreeSet instance. |
E | first() | Returns the current first (lowest) element in this set. |
E | floor(E e) | Returns the largest element in this set that is less than or equal to a given element, and if no such element returns null. |
NavigableSet<E> | descendingSet() | Returns an inverse view of the elements contained in this set. |
SortedSet<E> | headSet(E toElement) | Returns a partial view of this set whose elements are strictly smaller than toElement. |
NavigableSet<E> | headSet(E toElement, boolean inclusive) | Returns a partial view of this set whose elements are less than (or equal to, if inclusive is true) to Element. |
E | higher(E e) | Returns that the smallest element in this set is strictly larger than the given element, and if no such element returns null. |
E | last() | Returns the last (highest) element currently in this collection. |
Iterator<E> | iterator() | Returns an iterator of elements in this collection in ascending order. |
E | lower(E e) | Returns that the maximum element in this set is strictly smaller than the given element, and if no such element returns null. |
Test code:
package set; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; public class MySetStudy01 { public static void main(String args[]) { TreeSet<String> treeSet = new TreeSet<String>(); treeSet.add("1"); treeSet.add("3"); treeSet.add("3"); treeSet.add("2"); treeSet.add("-1"); treeSet.add("5"); PrintTreeSet(treeSet); System.out.println(treeSet.first()); System.out.println(treeSet.last()); System.out.println(treeSet.floor("2")); System.out.println(treeSet.higher("2")); System.out.println(treeSet.lower("2")); TreeSet<String> treeSet1 = (TreeSet<String>) treeSet.descendingSet(); PrintTreeSet(treeSet1); TreeSet<String> treeSet2 = (TreeSet<String>) treeSet.headSet("2"); PrintTreeSet(treeSet2); TreeSet<String> treeSet3 = (TreeSet<String>) treeSet.headSet("2", true); PrintTreeSet(treeSet3); } public static void PrintTreeSet(Set set) { Iterator iterator = set.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } System.out.println(); } }
Running screenshots are as follows:
END~