The structure of the collection is as follows:
The two top-level interfaces for a collection are: collection and map
Collection There are two more commonly used interfaces are list (list) and set (set), where list can store duplicate elements, elements are ordered (access order consistent), you can get the specified element through the list pin, and set can not have duplicate elements, elements are unordered.
List interface, the more commonly used classes have three: ArrayList, Vactor, LinkedList.
ArrayList: Threads are unsafe, queries on elements are faster.
Vector: Thread-safe, more of a way to remove elements: enumeration (enumeration), but has been replaced by ArrayList.
LinkedList: Linked list structure, the deletion of elements is very fast.
In the set interface, the more commonly used classes are two: HashSet, TreeSet:
HashSet: To guarantee element uniqueness, you need to override the Equals and Hashcode methods in object (because the underlying is the two methods to determine whether two elements are the same).
TreeSet: The elements are stored in the structure of a two-fork tree, and the elements can be sorted.
Two ways to sort:
1, the element itself has the comparison function, the element realizes the comparable interface, covers the CompareTo method.
2. Establish a comparator object that implements the comparator interface, overrides the Compare method, and passes the object as a parameter to the TreeSet constructor (which can be used with anonymous inner classes).
Map interface Its characteristics are: the elements are in pairs appear, in the form of keys and values, the key to ensure uniqueness: Commonly used classes are: Hashmap,hashtable, TreeMap.
HASHMAP: Thread unsafe, etc., allows null key to be stored null value.
Hashtable: Thread-safe, NULL key is not allowed to be stored.
TreeMap: You can sort the keys (to implement the Sort method with TreeSet).
Collection and map two interfaces are different for element operations:
Deposit elements:
The implementation class under the collection interface is done by the Add method, and the map is done by the put method.
remove element:
Collection interface: The list interface is available in two ways: 1, get (PIN), 2, get the element by iterator iteration, and vactor a way to enumerate (enumeration). The set interface obtains the elements in an iterative manner.
Map interface: Keyset gets the series of keys, and then uses the series to get the value of the element using the iterator iteration method.
Java collection common class features collation