Java Collection classes are mainly used to save and hold other data. Therefore, collection classes are also called container classes. Java Collection classes are divided into four major systems: set, list, map, and queue. Set indicates an unordered and non-repeating set. list indicates an ordered and reusable set. Map indicates a set with ing relationships; queue indicates a set of queues.
Java Collection classes are derived from two interfaces: Collection and Map, which are the root interfaces of the Collection framework. The following is the inheritance tree of its interfaces, subinterfaces, and implementation classes.
The following describes the four interfaces and their implementation classes.
Set interface. The set cannot contain the same elements. Set determines whether two objects are the same Based on the equals method. If the two objects return true using the equals method, set will not accept the two objects.
HashSet is a typical implementation of the set interface. HashSet uses the hash algorithm to store elements in the set. Therefore, it has good storage and search performance. HashSet judges that the equals methods of the two elements are equal, and the hasCode () method return values of the two objects are equal. HashSet can save null elements.
The List set represents an ordered set. Each element in the SET has its corresponding ordered index. Arraylist and vector are two typical implementations of the list interface. The notable difference between them is that vector is linear and secure, while arraylist is not. Both of them are list classes implemented based on arrays. List also has an external List Class Based on the linked List implementation. When you insert or delete an element, it is very fast. This class is special and has many features, that is, the List interface and Dueue interface (bidirectional Queue) are implemented ). It can be used as a two-way queue or a stack.
Queue is used to simulate the data structure of a Queue. Javaslist and ArrayDueue are two common implementation classes.
Map is used to save data with ing relationships. The Map interface has the following common implementation classes: HashMap, HashTable, and TreeMap. TreeMap sorts all keys in TreeMap Based on the red/black tree. There are two main differences between HashMap and HashTable: 1. Hashtable is linear and secure, so its performance is poor. 2. HashMap can use null as the key or value.
The Collection class also provides a tool class Collections. It is mainly used for searching, replacing, synchronization control, and setting immutable sets.
The above is a general overview of java Collection classes. The following describes the relationships among set, list, and map.
The relationship between Set and Map. All the keys in the Map set constitute a set. Therefore, the Map Set provides the set <K> keySet () method to return a Set set composed of all keys. It can be seen that all the keys in the Map set have the characteristics of the Set. As long as all the keys in the Map set are Set, it is a Set, which achieves the conversion from Map to set. At the same time, if we regard the elements in the Map as the set Set set of key-value, we can also convert them from Set to Map. HashSet and HashMap are their implementation classes respectively. The two are similar. The implementation of HashSet encapsulates HashMap objects to store elements. They are essentially the same. Similar to the relationship between HashSet and HashMap, in fact, TreeMap and TreeSet are similar in nature, and the underlying layer of TreeSet is also dependent on TreeMap implementation.
The relationship between Map and List. We can separate the key-value of the Map. from another perspective, we can unify the Map and List.
The Map Set is an associated array. The key can constitute a Set, and the values in the Map can be repeated. Therefore, these values can form a List Set. However, the values method of the actual Map does not return a List set. Instead, it returns a Collection set that does not store elements. In other words, the List set contains two sets of values, one of which is a virtual int index, the other group is the list set element. In this sense, the List is equivalent to a Map with all keys in the int type.
The following describes the differences between several similar classes.
ArrayList and rule list. ArrayList is a linear table stored in sequence, which is implemented by arrays at the underlying layer, and linear table stored in chained mode. In essence, it is a two-way linked list. ArrayList should be used for element operations that are frequently stored in Random storage, and sort list should be used for adding or deleting elements. But in general, the overall performance of ArrayList is better than that of rule list.
Performance options of HashSet and HashMap. There are two main aspects: capacity and load factor (size/capacity ). Low load factor increases data query performance, but reduces the memory overhead occupied by hash tables. If the load factor is relatively high, the query for data is usually frequent. Therefore, the initial capacity should be larger, but not too large. Otherwise, the memory space will be wasted.