java sets Map Collection

  • 2020-05-10 18:04:53
  • OfStack

Interface: red; Implementation class: black font

1. Collection set  

Collection

 |_____Set(HashSet)

 |      |_____SortedSet(TreeSet)

The & # 160; |, List (ArrayList, LinkedList, Vector)

 

Collection: the root interface in the collection hierarchy. JDK does not provide an implementation class for this interface.

List: ordered (in the order you put it in), repeatable, with subscripts.

Set: disordered, not repeatable, no subscript.

SortedSet: is a child of the Set interface, and the elements in SortedSet are ordered (alphabetically ascending).

The & # 160; The & # 160; The & # 160; The & # 160; The & # 160; The & # 160; The & # 160; The & # 160; The & # 160; The implementation elements are ordered through the compareTo method of the Comparable interface. All elements put in must implement the Comparable interface (or be accepted by the specified Comparator).

 

Differences between the implementation classes of the List interface:

ArrayList: essentially an array. Threads are not safe. Query (get/set) fast, add and delete (add/remove) slow.

LinkedList: essentially a bidirectional linked list. Threads are not safe. Queries (get/set) are slow, additions and deletions (add/remove) are fast.

Vector: almost identical to ArrayList, except that Vector is a synchronized class (synchronized), which is thread-safe.

 

2. Map collection

Map (HashMap Hashtable)

 |_____SortedMap(TreeMap)

 

Map: the key-value pair is stored. It cannot contain duplicate key, but can have duplicate value.

SortedMap: Map's child interface SortedMap is an Map of key in ascending order.

The difference between HashMap, Hashtable and TreeMap

HashMap: threads are not safe. Both key and value can be null. Elements are unordered. At the bottom is the hash table data structure.

Hashtable: thread safety. Neither key nor value can be null. Elements are unordered. At the bottom is the hash table data structure.

TreeMap: threads are not safe. Neither key nor value can be null. The elements are ordered (in alphabetical ascending order). At the bottom is a two-tree data structure.

 

3. How to avoid repetition of key and Set values of Map

 

Map put the same key, and the later one overrides the previous one

Map map=new  HashMap();

map. put (" name ", "3");

map. put (king "name", "5"); -- effective, covering the previous one

When Set puts the same element, the first one is valid, and the next one will not be put in

Set set=new HashSet();

set. add (" 111 "); - effective

set. add (" 222 "); Judge what you have, but don't put it in

1. put of HashMap and add of HashSet

The & # 160; Since the add() method of HashSet actually converts to calling HashMap's put() method to add key-value pairs when adding collection elements. The put() method of HashMap is first called.hashCode() to judge that the return value is equal, and if the return value is equal, true is also returned by equals comparison. Finally, it is believed that the key object is equal and already exists in HashMap.

2. put and   The TreeSet add

When the put method is called in TreeMap to add a key value, the   of the object is called; compareTo (or the & # 160; The compare) method compares all the keys. If the method returns to 0, the two keys are considered equal.

When TreeSet adds an element, it calls the compareTo or compare method to locate the location of the element, that is, it returns compareTo or compare and returns 0, which is considered as an element in the same position, that is, the same element


Related articles: