The characteristics of List, Set, Map in java collection and how to choose type in work

When it comes to the data structure of Java, the first thing we think of is definitely a collection, what map, list, and set. Let's talk about the characteristics and characteristics of these sets.

Parent interface of all collections:

Collection and Map are the parent interfaces of all collections.

  • List and Set inherit to Collection interface
  • Map itself is an independent interface

List interface:

  • There are ArrayList, LinkedList, and Vector under the List interface

Set interface:

  • There are HashSet, LinkedHashSet, and TreeSet under the Set interface

Map interface:

  • There are HashMap, LinkedHashMap, HashTable, and TreeMap under the Map interface

We can see the relationship between them through the figure below.
Insert picture description here

It should be noted that not only the Set interface and the List interface inherit the Collection interface, but the Queue interface also inherits the Collection.

Through the source code, we will find that the LinkedList interface not only implements the List interface, but also implements the Deque interface, and Deque inherits from Queue, and Queue interface inherits from Collection interface. The intricate relationships need to be read through the source code to understand more thoroughly.

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

Closer to home, continue with our previous topic and talk about the characteristics of these sets.

The characteristics of the collection:

The List is ordered. And repeatable


  • Advantages of ArrayList : The underlying data structure of ArrayList is an array, which is quick to query but slow to add or delete.
    Disadvantages: thread is not safe and efficient

Why does ArrayList query add and delete quickly?

Because the underlying data structure of ArrayList is an array, the array is the first address + (element length bai * subscript) in the query process, and the corresponding number of bytes can be read based on this position, so it is very fast. Additions and deletions will bring about the movement of elements, adding data will move backward, and deleting data will move forward, which affects efficiency.

  • LinkedList
    advantages: The underlying data structure of LinkedList is a linked list, which is fast to add and delete but slow to query. Thread safety.
    Disadvantages: Because it is thread-safe, it is inefficient.

Why does LinkedList add and delete quickly and query slowly?

The underlying data structure of LinkedList is a linked list, and the linked list storage object is placed in a separate link (link), and the link also stores the reference of the next link. In the java language design, the linked lists are all two-way links (Double linked) -That is, each object also stores the reference of this precursor, so data additions and deletions will not move the position of other elements. Additions and deletions are faster than arrays. However, the query can only be accessed through sequential pointers, so the query is slower.

  • Vector
    advantages: The underlying data structure of Vector is an array, which is fast in query but slow in addition and deletion. Thread safety.
    Disadvantages: Because it is thread-safe, it is inefficient.

Set unique


  • Advantages of HashSet : The underlying data structure of List is a hash table. The uniqueness of the element is guaranteed through hashCode() and equals().
    Disadvantages: disorder


  • Advantages of LinkedHashSet : The underlying data structure of LinkedList is a linked list and a hash table, which are ordered. The order of the elements is guaranteed through the linked list, and the uniqueness of the elements is guaranteed through the hash table.
    Disadvantages: Since insertion is to ensure the order of elements, the efficiency is lower than HashSet.

  • TreeSet
    advantages: The underlying data structure of Vector is a red-black tree. Due to the characteristics of red-black trees, TreeSet is ordered, and the uniqueness of the element is determined by comparing whether the return value of the element is 0.

Map KV key-value pair storage


  • Advantages of HashMap : After JDK1.8, the underlying data structure is array + linked list + red-black tree, with unique keys. A null key and null value are allowed.
    Disadvantage: thread is not safe
  • LinkedHashMap
    advantages: Inherited to HashMap to ensure the insertion order of elements.
    Disadvantages: Since insertion is to ensure the order of elements, the efficiency is lower than HashSet.
  • HashTable HashTable's underlying data structure is a hash table, thread safety
    advantages: low efficiency
  • The underlying data structure of TreeMap is red-black tree, natural sorting,
    advantages: lower efficiency than HashMap

Collection selection

It is inevitable to use collections in our work, so how do we choose so many collection types?
This needs to be decided according to our use needs.

Do you need to store key-value pairs
——No: Set or List
————Is the data unique
——————Yes: Set
——————Is it ordered
————————Yes, use LinkedHashSet , TreeSet
———————— No, use HashSet
———————— If you only know that the data is unique but not sure whether it is in order, HashSet
—————— No: List
—— —————— Is thread safe
—————————— Yes: Vector
—————————— No: ArrayList, LinkedList
————————————Query multi ArrayList
------------ additions multi LinkedList
------------ you know if you use List, but do not know more or deletions and more queries to use ArrayList
- is Map
————Is it thread safe
——————Yes: HashTable
——————No: Whether to allow null keys
————————Yes: HashMap
————————No : LinkedHashMap, TreeMap
———————— If you know how to use Map but don’t know which one to use, use HashMap

Reference article: https://blog.csdn.net/zhangqunshuai/article/details/80660974

Guess you like

Origin blog.csdn.net/nxw_tsp/article/details/109145836