Map,Set,List

1. Common Ground:

Set and List are packages under java.util that implement the Collection interfaceCopy the code

2. The difference between:

The list:

1).ArrayList: ArrayList is a variable array (2) that can insert multiple null values (3) Size, isEmpty, get, set Iterator,add(E) operation time complexity is O(1) (4)ArrayList is not synchronized (5) is often used in data query (6)ArrayList does not allocate memory during initialization, but only allocates memory space of size 10 when adding data, as shown in figure:Copy the code

When the data reaches the length of the array, we call Array.copyof to copy a longer array and put the previous data in. As shown in figure:Copy the code

As the number of elements increases, create a new array 1.5 times the length of the original array, 10, 15, 22, 33,. The sequence is created by copying the original elements into the new array. If the array reaches the maximum length, MAX_ARRAY_SIZE or integer. MAX_VALUE is used as the maximum length, and the extra elements are discarded. (7) arrayList. remove time complexity O(n) 2).linkedList: (1)LinkedList is a sequence container maintained by a LinkedList (2) multiple null values can be inserted (3)add method operation time complexity is O(1) (4) two-way list, no initialization size (5)LinkedList underlying method implements Deque interface, The Deque interface inherits from Queue, so it also supports pop, push, peek operations on queues.Copy the code

Set:

1) HashSet: (1) The underlying hash table is a hash table, so it cannot store the same value. The values in the set are stored at the key position in the map, and the values are all the sameCopy the code

(2) Because it is implemented based on a hash table, its related methods are all operations on the hash table. (2) LinkedSet: consists of both a linked list and a hash table. 3) TreeSet is an ordered collection. When no order is set, the TreeSet is naturally sorted according to the Comparator. (Natural ordering is e1.compareTo(e2) == 0 for comparison.) Elements within a TreeSet must implement the Comparable interface.Copy the code

Map

1) a HashMap: hash table to store related principle: https://blog.csdn.net/tuke_tuke/article/details/51588156 2) LinkedHashMap: (1) LinkedHash descends from HashMap, so its underlying structure is still based on a pull-hash structure. This structure consists of array and linked list + red-black tree. On this basis, LinkedHashMap adds a two-way linked list. The problem of keeping the traversal order and insertion order consistent (2) is inherited from hashMap so the initial capacity is also 16 (3) LinkedHashMap has no PUT method and uses the put method entirely from its parent hashMap except that it is different when architecting the linked list. Concretely reflected in the figure below:Copy the code

(4) Iterate directly from the table header. It's ordered traversal. Because maintain a head, tail linked listCopy the code

3) TreeMap is not commonly used. TreeMap will implement the SortMap interface and define a sorting rule, so that when TreeMap is traversed, elements will be returned according to the specified sorting rule. 4) WeakHashMapCopy the code