The
2.Set Usage Scenarios
API describes: The
Java.util.Set interface, like the Java.util.List interface, inherits from the collection interface, and
It is basically consistent with the methods in the collection interface. There is no functional expansion of the collection interface, and the
is only more stringent than the collection interface. Unlike the list interface, the elements in the set interface are unordered, and the
will ensure that the elements deposited are not duplicated in a certain rule. The set collection has more than one subclass, and
Here we describe the two collections of
Java.util.HashSet,
Java.util.LinkedHashSet.
Feature: All elements stored in a rule are guaranteed to be non-duplicated, elements in the set interface are unordered
① elements are not duplicated
② elements unordered (refers to access unordered)
Hash Table: The class to which the custom object belongs is as long as the custom object is stored in the hash table The Hashcode&equals method
⑴hashset Collection
must be overridden by the underlying new HashMap object implemented through HashMap, using only key and not using value value to point to the unified address new object ()
API Description:
Java.util.HashSet is an implementation class of the set interface that stores elements that are non-repeatable,
, and whose elements are unordered (that is, the access order is inconsistent). Java.util.HashSet the implementation of the underlying is actually
a JAVA.UTIL.HASHMAP support, HashSet is based on the object's hash value to determine where the element is stored in the collection,
and therefore has good access and lookup performance. The way to guarantee element uniqueness depends on: Hashcode and the Equals method.
Feature:
① element does not repeat
② access order inconsistent
③ good access and lookup performance
④ element uniqueness depends on: hashcode and Equals method
HashSet The structure (hash table) of the collection store data:
Before the JDK1.8, the hash table was implemented using an array + chain list, which used the list to handle conflicts,
A linked list of the same hash value is stored in a list. But when there are more elements in a bucket,
That is, when the hash value is equal to the number of elements, through the key value search efficiency is low.
In JDK1.8, hash table storage is implemented using array + linked list + red black tree,
When the list length exceeds the threshold (8), the linked list is converted to a red-black tree, which greatly reduces the lookup time.
In a word, JDK1.8 introduced red-black trees to optimize the performance of HashMap,
So for us, the only thing that guarantees the HashSet collection element is that it is based on the object's
Hashcode and the Equals method to determine. If we store the custom object in the collection,
So to guarantee its uniqueness, it must be replicated hashcode and the Equals method to establish a comparison of the current object.
Summary: 1.8 is implemented by using the array + chain list at the bottom of the hash table
1.8 is implemented through hash table storage using array + linked list + red black tree implementation (the list length exceeds the threshold (8), the linked list is converted to a red black tree, which greatly reduces the time to find)
To guarantee that the element is unique, you must override the Hashcode and equals methods to establish a comparison of the current object
HashSet Store Custom type elements (emphasis):
When storing custom type elements in HashSet, you need to override the hashcode and Equals methods in the object.
Establish your own comparison method to ensure that the objects in the HashSet collection are unique
⑵linkedhashset
HashSet ensure that elements are unique, but the elements are stored in no order, then we must ensure orderly, how to do?
Under HashSet there is a subclass Java.util.LinkedHashSet, which is a data storage structure for the combination of linked lists and hash tables.
Usage Scenarios for Java set