[Java-Java Collection] Detailed Explanation and Difference of Java Collection

1 Overview

1. All collection classes and collection interfaces are under the java.util package.

2. Apply for a piece of space in memory to store data. In Java, a collection is a reference data type that replaces a fixed-length array.

3. There are three main collection types: Set (set), List (list) and Map (map) . Of course, there are some low-level collections such as: Vector, Stack, Queue, etc., but they are not used much in daily use. Here we only introduce the commonly used collections in development.

4. The collection interface is divided into: Collection and Map (List and Set implement the Collection interface)

2) Collection frame diagram

2.1. Overall frame diagram

insert image description here

2.2. Iterable frame diagram

insert image description here

2.3.Map frame diagram

insert image description here

3)List

1. It can be repeated and ordered . The data can be retrieved and added through the index. The order is consistent with the insertion order and can contain null elements.

2. The List collection is divided into two categories, ArrayList, LinkedList , and Vector (not commonly used).

  • ArrayList: The underlying data structure is the array structure array, which has fast query speed and slow addition, deletion and modification. Because it is stored in an array-like form, its random access speed is extremely fast and thread is not safe .

  • LinkedList: The bottom layer uses a linked list structure, which is fast in addition and deletion, and slightly slower in query. Each element of LinkedList is connected to each other. Once the data is queried, it is time-consuming to start the query from the beginning every time. However, adding, deleting, modifying and checking can accurately find the corresponding position, and the thread is not safe .

  • Vector: The bottom layer is an array structure array, which is the same as ArrayList, with fast query speed, slow addition, deletion and modification, and thread safety .

3. The difference between ArrayList and Vector:

  • If the number of elements in the collection is greater than the length of the current collection array, the growth rate of Vector is 100% of the current array length, and the growth rate of ArryaList is 50% of the current array length. Therefore, if a large amount of data is used in the collection, using Vector has certain advantages.

  • Thread synchronization ArrayList is not thread synchronized, so Vector is thread-safe, but because each method is synchronized, it is less efficient than ArrayList.

4. Common traversal methods for collections:

  • normal for loop

  • Enhanced for loop

  • iterator traversal

3.1.ArrayList class inheritance diagram

insert image description here

3.2. LinkedList class inheritance diagram

insert image description here

4)Set

1. The data cannot be repeated (unique), out of order, and the implementation class is not a thread-safe class . Solution: Set set = Collections.sysnchronizedSet (Set object)

2. Set collections are roughly divided into two categories: HashSet, LinkedHashSet, and TreeSet .

  • HashSet

    It is the most commonly used implementation class of the Set interface (the Set interface inherits the Collection interface). As the name suggests, the underlying layer uses a hash table (hash/hash) algorithm . Its bottom layer is actually an array. The meaning of its existence is to provide query speed, and the insertion speed is relatively fast, but it is suitable for the insertion operation of a small amount of data, and the thread is not safe .

  • LinkedHashSet

    It inherits the HashSet class, so its bottom layer also uses the data structure of the hash table, but because the order of adding data is maintained, a linked list structure is added, but because an additional data structure is added, the efficiency is low . It is recommended to use LinkedHashSet if you need to ensure that elements are not repeated in a set, and you also need to record the order in which elements are added before choosing to use LinkedHashSet, which is not thread-safe .

  • TreeSet

    The implementation class of the Set interface also has the general characteristics of the Set interface, but the difference is that it also implements the SortSet interface, and its underlying layer uses the red-black tree algorithm (a red-black tree is a binary search tree that satisfies the red-black nature). , it should be noted that only references to objects of the same type can be stored in the TreeSet collection, which is not thread-safe .

3. The most important thing about Tree is its two sorting methods: natural sorting and client sorting .

  • 自然排序: Implement the Comparable interface, so TreeSet can call the ComparableTo() method of the object to compare the size of the collection, and then sort in ascending order. This sorting method is called natural sorting. Among them, the Comparable interface is implemented as well as BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short (sorted according to the size of the number), Character (sorted according to the size of the number of the Unicode value) String (according to the Unicode of the characters in the string values ​​to sort) classes, etc.

  • 客户化排序: In fact, it is to implement the specific sorting method provided by the java.util.Comparator interface. It is the type of objects to be compared. It has a compare method. For example, compare(x, y) returns a value greater than 0 to indicate that x is greater than y. By analogy, when we want to sort according to our own ideas, we can override the compare method.

4. Common traversal methods for collections:

  • normal for loop

  • Enhanced for loop

  • iterator traversal

4.1. HashSet class inheritance diagram

insert image description here

4.2. LinkedHashSet class inheritance diagram

insert image description here

4.3. TreeSet class inheritance diagram

insert image description here

5)Map

1. Map is a collection of mapping key objects and value objects. Each element contains key objects and value objects, and the value objects can also be data of Map type. Therefore, Map supports multi-level mapping. Map The keys in are unique, but the values ​​can be non-unique .

2. Map is roughly divided into three categories: HashMap, TreeMap, HashTable (not commonly used) .

  • HashMap: (unordered)

    Both it and HashSet use a hash table to complete. The difference is that in each bucket of the hash table, HashSet only has a key, while HashMap hangs a value on each key, which is not thread safe .

  • TreeMap: (ordered)

    It implements the SortMap interface, that is, it uses the data structure of the red-black tree . Like the TreeSet, it can also implement two sorting methods: natural sorting and custom sorting , while the hash table does not provide sorting and is not thread-safe .

  • HashTable

    Hashtable inherits the Map interface and implements a key-value mapping hash table. Any non-null object can be used as key or value, thread-safe .

3. Common traversal methods for collections:

  • KeySet(): It is to put all the Keys in the Map together, and then get the corresponding Value through get(key).

  • EntrySet(): Put the Key-Value as a whole into a Set, and get the corresponding Key and Value through the getKey() and getValue() methods.

5.1. HashMap class inheritance diagram

insert image description here

5.2. TreeMap class inheritance diagram

insert image description here

5.3. HashTable class inheritance diagram

insert image description here

6) Collection comparison

insert image description here

detailedhttps://blog.csdn.net/u010775025/article/details/79315361

Guess you like

Origin blog.csdn.net/weixin_53543905/article/details/130259272