Chapter 12 Summary

12.1 Overview of collections

The java.util package provides some collection classes, which are also called containers. When it comes to containers, it is not difficult to think of arrays. The difference between a collection class and an array is that the length of an array is fixed, while the length of a collection is variable; arrays are used to store basic types of data, and collections are used to store references to objects. Commonly used collections include List collection, Set collection and Map collection. List collection and Set collection inherit the Collection interface, and each interface also provides different implementation classes. The inheritance relationship of the above collection classes is shown in Figure 12.1.

12.2Collection interface

The Collection interface is the root interface in the hierarchy, and the units that make up a C collection are called elements. The Collection interface usually cannot
be used directly, but the interface provides methods for adding elements, deleting elements, and managing data. Since both the List interface and the Set interface inherit the Collection interface, these methods are common to the List collection and the Set collection. The common methods of the Collection interface are shown in Table 12.1.    

12.3List collection

The List collection includes the List interface and all implementation classes of the List interface. The elements in the List collection are allowed to be repeated. The order of each element is the order in which the object is inserted. It is an array of type Java. Users can access the elements in the collection by using indexes.

12.3.1List interface

The List interface inherits the Collection interface and therefore contains all methods in the Collection interface. In addition, the List interface also defines two very important methods:

get(int index): Get the element at the specified index position

set (int index, Object obj): Modify the object at the specified index position in the collection to the specified object.

12.3.2 Implementation class of List interface

The ArrayList class implements a mutable array, allowing storage of all elements, including null, and fast random access to the collection based on index position. The disadvantage is that inserting or deleting objects to the specified index position is slower.

The LinkedList class uses a linked list structure to save objects. The advantage of this structure is that it facilitates the insertion and deletion of objects into the collection. When objects need to be inserted or deleted from a collection, the List collection implemented using the LinkedList class is more efficient; however, for random access to objects in the collection, the List collection implemented using the LinkedList class is inefficient.

List<E>list=new ArrayList<>();

List<E>list2=new Linkedlist<>();

E can be a legal Java data type.

 

12.4Set collection

The objects in the Set collection are not sorted in a specific way. The objects are simply added to the collection, but the Set collection cannot contain duplicate objects. The Set collection consists of the Set interface and the implementation class of the Set interface. The Set interface inherits the Collection interface and therefore contains all methods of the Collection interface.

The HashSet class implements the Set interface and is supported by a hash table. It does not guarantee the iteration order of the Set collection, especially when it does not guarantee that the order will remain unchanged. This class allows null elements. Note: The same hash address can store multiple different objects

The TreeSet class not only implements the Set interface, but also implements the java.util.SortedSet interface. Therefore, the Set collection implemented by the TreeSet class is sorted incrementally according to the natural order when traversing the collection, or it can also be sorted incrementally according to the specified comparator, that is, it can be sorted through the comparator. Use the TreeSet class to sort objects in a Set collection.

New methods added to TreeSet class

12.5Map collection

Map collection does not inherit the Collection interface, which provides a mapping from key to value. The Map collection cannot contain the same key, and each key can only map one value. The key also determines the storage location of the storage object in the mapping, but it is not determined by the key object itself
, but is processed through a "hash technology" to generate a hash code an integer value. The hash code is often used as an offset that corresponds to the starting position of the memory area allocated to the map, thereby determining where the stored object is stored in the map. The Map collection includes the Map interface and all implementation classes of the Map interface.    

12.5.1Map interface

The Map interface provides objects that map keys to values. A mapping cannot contain duplicate keys, and each key can only be mapped to at most one value. In addition to the common methods of collections,

12.5.2 Implementation class of Map interface

The HasMap class is an implementation of the Map interface based on a hash table. This implementation provides all optional mapping operations and allows the use of null values ​​and null keys, but the uniqueness of the keys must be guaranteed. The HashMap class uses a hash table to quickly search for its internal mapping relationships. This class does not guarantee the order of the mapping, and in particular it does not guarantee that the order is immutable.
The TreeMap class not only implements the Map interface, but also implements the java.util.SortedMap interface, so the mapping relationships in the collection have a certain order. However, the TreeMap class performs slightly worse than the HashMap class when adding, deleting, and positioning mapping relationships. Since the mapping relationships in the Map collection implemented by the TreeMap class are arranged in a certain order according to the key objects, the key objects are not allowed to be null.
It is recommended to use the HashMap class to implement the Map collection, because the Map collection implemented by the HashMap class is more efficient in adding and deleting mapping relationships. You can create a Map collection through the HashMap class. When sequential output is required, create another one that completes the same mapping relationship

Guess you like

Origin blog.csdn.net/Leonie1/article/details/132836421