Interface: red; implementation class: black font
1. Collection collection
Collection
|_____ Set (HashSet)
| |_____ SortedSet (TreeSet)
|_____ List (ArrayList, LinkedList, Vector)
Collection : The root interface in the collection level. JDK does not provide an implementation class for this interface.
List : Orderly (in the order of placement), can be repeated, and has subscripts.
Set : Unordered, non-repeatable, no subscript.
SortedSet : is a subinterface of the Set interface. The elements in the SortedSet are ordered (in ascending order in alphabetical).
Through the Comparable interface's compareTo method, the elements are implemented in an orderly manner. All elements placed must implement the Comparable interface (or be accepted by the specified Comparator).
Differences between implementation classes of List interface:
ArrayList : is essentially an array. Threads are not safe. Query (get/set) is fast, add/remove (add/remove) is slow.
LinkedList : Essentially a two-way linked list. Threads are not safe. Query (get/set) is slow, add/remove is fast.
Vector : It is almost exactly the same as ArrayList. The only difference is that Vector is synchronized and is thread-safe.
2. Map collection
Map (HashMap, Hashtable)
|_____ SortedMap (TreeMap)
Map : It stores a key-value pair, which cannot contain duplicate keys, and can have duplicate values.
SortedMap : Map's subinterface SortedMap is a map that arranges keys in ascending order.
The difference between HashMap , Hashtable and TreeMap
HashMap : Threads are not safe. Both key and value can be null. The elements are disordered. The underlying layer is the hash table data structure.
Hashtable : thread-safe. Neither key nor value can be null. The elements are disordered. The underlying layer is the hash table data structure.
TreeMap : Threads are not safe. Neither key nor value can be null. The elements are ordered (in ascending alphabetical order). The underlying layer is the binary tree data structure.
3. How to implement the key and Set values of Map without duplication
Map puts the same key , and then covers the previous one
Map map=new HashMap();
map.put("name","Zhang San");
map.put("name","wangwu"); -------Effective, overwrite the previous
When Set puts the same element, the first one is valid, and the next one will not be put into
Set set=new HashSet();
set.add("111");---Effective
set.add("222");----Judge that it has already existed and will not be put in
1. HashMap put and HashSet add
Because HashSet's add() method actually turns to call HashMap's put() method to add key-value pairs when adding a collection element. The put() method of HashMap first calls .hashCode() to determine that the return value is equal. If the return value is equal, then it returns true through equals comparison. Finally, it is believed that the key object is equal and already exists in HashMap.
2. TreeMap put and TreeSet add
When the put method is called in TreeMap to add key value, the compareTo (or compare) method of the object is called to compare all keys. If this method returns 0, the two keys are considered equal.
When adding an element in TreeSet, call compareTo or compare method to locate the position of the element, that is, return compareTo or compare to return 0, which is considered to be the same position, that is, the same element