Java Basics - Summary of Collection single-column collection and Map double-column collection

(1) Collection class architecture:

  • Collection is a single-column collection, each element (data) contains only one value.
  • Collection format: [element 1, element 2, element 3...].
  • Map double-column collection, each element contains two values ​​(key-value pairs).
  • The complete format of the Map collection: {key1=value1, key2=value2, key3=value3...}.

 (2) The Collection collection system is as follows (the ones without borders are interfaces, and the ones with borders are implementation classes):

(3) Collection features:

1. List series collection: (ArrayList, LinkedList) added elements are ordered, repeatable, and indexed.

  1. Ordered: Store and retrieve elements in the same order.
  2. With index: Elements can be manipulated by index.
  3. Repeatable: Stored elements can be repeated.

2. Set series collection: (HashSet) The added elements are unordered, non-repetitive, and without index.

  1. Unordered: The access sequence is inconsistent.
  2. No Duplication: Duplicates can be removed.
  3. No index: There is no method with index, so ordinary for loop traversal cannot be used, nor can elements be retrieved by index.
  • LinkedHashSet: ordered, not repeated, no index.
  • TreeSet: Sort in ascending order by default by size, no repetition, no index.

(4) The Map collection system is as follows (the ones without borders are interfaces, and the ones with borders are implementation classes):

  •  The most used Map collection is HashMap.
  • Focus on mastering HashMap, LinkedHashMap, TreeMap.

(5) Features of the Map collection system:

  • The keys of the Map collection are unordered and not repeated.
  • The value of the Map collection is not required and can be repeated.
  • The value corresponding to the repeated key after the Map collection will overwrite the value of the previous repeated key.
  • The key-value pairs of the Map collection can be null.

(6) Features of the Map collection implementation class:

  • HashMap: The elements are unordered according to the key, no repetition, no index, and no requirement for the value. (Consistent with the Map system)
  • LinkedHashMap: The elements are ordered according to the key, no repetition, no index, and no value requirement.
  • TreeMap: elements are sorted according to the key, no repetition, no index, and no value requirement.

1.HashMap

Features:

  • The characteristics are determined by the key: unordered, not repeated, no index.
  • There is no additional learning method, just use the method in Map directly.
  • The underlying principles of HashMap and HashSet are exactly the same, both are the results of hash tables, except that each element of HashMap contains two values ​​(the bottom layer of the Set series collection is implemented by Map, but the Set collection only needs key data, not value data).
  • Rely on the hashCode method and the equals method to ensure the uniqueness of the key (if the key is to store a custom object, you need to override the hashCode method and the equals method).

2.LinkedHashMap

Features:

  • Determined by the key: ordered (to ensure that the elements stored and retrieved are in the same order), no repetition, no index
  • Principle: The underlying data structure is still a hash table, but each key-value pair element has an additional double-linked list mechanism to record the order of storage.

3.TreeMap

Features:

  • Sortable: Sort according to the default ascending order (from small to large) according to the size of the key data. Only keys can be sorted. (The TreeMap collection must be sorted. It can be sorted by default, or the keys can be sorted according to the specified rules == "custom sorting rules: 1. The class implements the Comparable interface and rewrites the comparison rules. 2. Set custom Comparator comparators object, overriding the comparison rules.)
  • TreeMap has the same underlying principle as TreeSet.

 

Guess you like

Origin blog.csdn.net/weixin_61275790/article/details/130050195