The difference between the three sets of Set, Map and List

The difference and connection of List, Map, Set

1. Structural features

ListAnd Setare stored in a separate data set, Mapis a set of key-value pairs stored in the double row of such data;

  • The data stored in the List is in order, and the value is allowed to repeat;
  • The data stored in the Map is unordered, its keys are not allowed to be repeated, but the values ​​are allowed to be repeated;
  • The data stored in the Set is unordered and does not allow repetition, but the position of the element in the set is determined by the hashcode of the element, that is, the position is fixed (the Set collection is based on the hashcode for data storage, so the position is fixed Yes, but this position is not user-controllable, so the elements in the set are still out of order for the user).

Second, the implementation class

  1. The List interface has three implementation classes
    1.1 LinkedList
    Based on the linked list implementation, the linked list memory is hashed, adding and deleting fast, and searching slowly;
    1.2 ArrayList
    Based on the array implementation, non-thread safe, high efficiency, slow adding and deleting, and looking up fast
    1.3 Vector
    Based on the array implementation, thread safety, efficiency Low, slow addition and deletion, slow search
  2. There are four implementation classes for the Map interface.
    2.1 Implementation HashMap
    of the Map interface based on the hash table, which is non-thread-safe and efficient, supports null values ​​and null keys;
    2.2 HashTable
    Thread-safe, inefficient, does not support null values ​​and null keys
    2.3 LinkedHashMap
    is a subclass of HashMap , Saves the insertion order of the records.
    2.4 SortMapinterface
    TreeMap, which can sort the records it saves according to the key, the default is the ascending order of the key value
  3. There are two implementation classes for the Set interface.
    3.1 The HashSet
    bottom layer is implemented by Hash Map, which does not allow duplicate values ​​in the set. When using this method, you need to rewrite the equals() and hashCode() methods;
    3.2 LinkedHashSet
    inherits from HashSet and is based on LinkedHashMap. For implementation, the underlying layer uses LinkedHashMap

Three, the difference

  1. The objects in the List collection are sorted according to the index position, that is, the subscript. There can be duplicate objects, allowing retrieval of objects according to their index positions in the collection. (Want to get such as: list.get( i ));
  2. The objects in the Set collection are not sorted in a specific way, and there are no duplicate objects.
  3. Each element in the Map contains a key and a value, which appear in pairs. The key object can be repeated

Four, supplementary knowledge

How is LinkedList implemented?

  1. ArrayList is a data structure based on a dynamic array, and LinkedList is a data structure based on a linked list

  2. For random access to get and set, ArrayList feels better than LinkedList to move the pointer

  3. For adding and deleting operations add and remove, LinkedList is more dominant, because ArrayList needs to move data.
    It depends on the situation:
    if only a single piece of data is inserted or deleted, the speed of ArrayList is due to LinkedList.
    If it is a batch of random insertion and deletion of data , LinkedList is much faster than ArrayList. Because ArrayList inserts a piece of data, it must move the insertion point and all data after it.

  4. The bottom layer is a doubly linked list structure, so it is easy to add and delete, but it is slow to read and write, because it needs to start from the head of the linked list every time you look up, and the bottom layer of arrayList is an array, and the array has its subscript, because the array is in There is a fixed length when declaring, so when data is added or deleted, it will redeclare a new array, which will result in inefficient addition and deletion.

Guess you like

Origin blog.csdn.net/MiaoWei_/article/details/109198693