Differences and implementation methods of List, Set, and Map in Java

Differences and implementation methods of List, Set, and Map in Java

List

  • List is an ordered collection, that is, the elements are sorted in the order of insertion, and there can be duplicate elements.
  • Because it is ordered, you can get elements according to the subscript or traverse the elements in the entire collection.
  • Commonly used implementation classes include ArrayList and LinkedList.

ArrayList

  • The bottom layer is implemented based on an array, and an Object[] array is maintained internally.
  • When you need to add elements, first check whether the array is full, if not, add elements directly later, otherwise you need to increase the capacity by expanding the array.
  • Since the length of the array is fixed and the elements in the array are continuous, the time complexity of querying an element is O(1), and the time complexity of adding or deleting an element is O(n) (the following elements need to be moved).

LinkedList

  • The bottom layer is implemented based on a linked list, each node contains an element and a reference to the next node.
  • When you need to add (add O(1) at the end) or delete (delete the head node or use the iterator's remove method O(1)) elements, you only need to modify the references between adjacent nodes, and you don't need to move other elements . This makes LinkedList faster than ArrayList in adding or removing elements.
  • Since there is no continuous memory, and the entire linked list needs to be traversed to find the specified element, the time complexity of querying an element is O(n), while the time complexity of adding or deleting an element is O(1).

Set

  • Set is a collection that does not allow duplicate elements, and the elements are in no particular order.
  • It can be used to judge whether an element has been present in the collection ¥¥.
  • Commonly used implementation classes include HashSet and TreeSet.

HashSet

  • The bottom layer is implemented based on HashMap, and a HashMap instance is maintained internally as its member variable.
  • When adding an element, the element is stored as a key in the HashMap, and the value is a fixed constant object.
  • Since the bottom layer of HashMap uses a hash table, it can quickly find out whether an element already exists in the collection, and the time complexity is O(1).
  • Traversal order is not guaranteed, nor insertion order.

TreeSet

  • The bottom layer is implemented based on the red-black tree, and each element must implement the Comparable interface or pass a Comparator object to the constructor.
  • Each node corresponds to an element, and each node has the following properties:
    • If a node has a left child node, all elements on the left child node are smaller than the elements on the node;
    • If a node has a right child node, all elements on the right child node are larger than the elements on the node;
    • The left and right subtrees are themselves a binary search tree.
  • Since the bottom layer of the TreeSet uses a red-black tree, the time complexity of adding elements, deleting elements, and finding elements is O(logn) on average.
  • Ensure elements are sorted in ascending order, or customize the sorting by passing a Comparator instance on creation.

Map

  • Map is a collection of key-value pairs, which allows both the key and the value to be null, but the key cannot be repeated, and the value can be repeated.
  • It can be used to store some highly correlated data objects, such as phonebooks, dictionaries, etc.
  • Commonly used implementation classes include HashMap and TreeMap.

HashMap

  • The bottom layer is also implemented based on a hash table. An array is maintained internally, and each element is the first node of a linked list or tree, which is used to resolve hash conflicts.
  • When adding an element, it will be hashed according to the hash value of the key, and then the corresponding array position will be found. If the element already exists at this position, it will be inserted in the form of a linked list or a tree structure.
  • HashMap can quickly find out whether the value corresponding to a key exists in the set, and the time complexity is O(1) (if the hash function is well designed).
  • Neither traversal order nor insertion order is guaranteed.

TreeMap

  • The bottom layer is implemented based on a red-black tree, and each key-value pair is encapsulated into an Entry object, which is sorted according to the natural order of the keys or the specified Comparator.
  • All elements in the TreeMap are guaranteed to be arranged according to the sorting rules, and an ordered list of key-value pairs can be obtained when traversing the TreeMap.
  • The time complexity of adding, deleting and finding elements is O(logn), where n represents the number of elements.
  • TreeMap can customize the sorting method, and supports the restriction that only key types that implement the Comparable interface are allowed.

Summarize

List

List is the most basic and commonly used data structure in the Java collection framework. It is an ordered collection that allows repeated elements. List provides operations such as inserting, deleting, and getting elements at specified positions according to the index.

There are many implementation classes for List in Java, the more commonly used ones are:

  • ArrayList: based on array implementation, and dynamic expansion.
  • LinkedList: Based on linked list implementation, it is suitable for frequent addition and deletion of elements.

Set

Set is also a data structure in the Java collection framework. It is an unordered collection composed of different elements and does not allow repeated elements. The main purpose of Set is to eliminate duplicate elements.

The implementation classes of Set in Java are:

  • HashSet: Implemented based on a hash table, it can quickly determine the uniqueness of an object.
  • TreeSet: Based on the implementation of red-black tree, the elements can be sorted and the uniqueness of elements can be guaranteed.
  • LinkedHashSet: Based on hash tables and linked lists, it retains the order of insertion and ensures the uniqueness of elements.

Map

Map is also the most commonly used data structure in the Java collection framework. It is a collection of key-value pairs. Each key can only appear once, and each key can only be mapped to one value.

There are many implementation classes for Map in Java, the more commonly used ones are:

  • HashMap: Based on the implementation of the hash table, it is stored and accessed in the form of key-value pairs.
  • TreeMap: Based on the red-black tree implementation, the keys can be sorted and the uniqueness of the keys can be guaranteed.
  • LinkedHashMap: Based on the implementation of hash tables and linked lists, the order of elements is maintained in the order of insertion.

Guess you like

Origin blog.csdn.net/qq_45450889/article/details/131226840