1. Collection class diagram
Collection
Shortlist
│ Invalid parameter list
│ ├ Arraylist
│ Vector
│ Elastic Stack
Sorted set
├ Hashset
├ Treeset
├ Linkedhashset
Map
├ Hashtable
├ Hashmap
├ Weakhashmap
└ Sortedmap
└ Treemap
2. Category Introduction
Collection is the most basic collection interface,All classes provided by JDK inherit from the "sub-interfaces" of collection, such as list and set.All classes that implement the collection interface must provide two standard constructor: A constructor without parameters and a constructor with a collection parameter. The former is used to create an empty collection, and the latter is used to create a new collection, allowing users to copy a collection.Regardless of the actual type of collection, it supports an iterator () method. This method returns an iterator that can access each element in the collection one by one. The usage is as follows:
Iterator it = collection. iterator ();//Get an iterator
While(It. hasnext ()){
Object OBJ = it. Next ();//Obtain the next element.
}
The two interfaces derived from the collection interface are list and set.
1) differences between collection and map:
- Collection type, each location has only one element.
- Map type, holding data in key-value format --"Key-Value Pair", that is, its elements are paired objects.
2) differences between list and set of collection subinterfaces:
- List is an ordered collection,Ensure that the specific sequence of elements is maintained,This is similar to the Java array.
- The Set interface does not guarantee the order of maintenance elements,Each element stored in the set must be unique..The equals () method must be defined for the object added to the set to ensure the uniqueness of the object.
3)List Implementation classDifferences between arraylist, sorted list, and vector
- ArraylistAn array of variable sizes is implemented,Fast Random Access to elements is allowed, but it is slow to insert and remove elements into the list.(Similar to dynamic arrays ).It allows all elements, including null. Arraylist is not synchronized. If the length of an array is certain, it is more efficient to use an array.
- The overhead of inserting and deleting elements in the shortlist is small, and random access is relatively slow.(Similar to linked list ). It can be used in stacks, queues, and bidirectional queues.
- VectorIs synchronized.When an iterator is created and in use, and another thread changes the vector state, concurrentmodificationexception is thrown, so this exception must be caught. It is slower than arraylist.
- Stack inherits from vector to implement a post-import, first-out stack. Synchronous.
4) differences between set implementation class hashset and treeset
- Hashset is a set (using a hash function) designed for fast search ). The object stored in the hashset must define hashcode (). It does not guarantee the iteration sequence of the Set; in particular, it does not ensure that the order remains unchanged. This class is allowedNullElement. This implementation is not synchronous.
- Treeset maintains the order of the set, the underlying isRed/black tree. It can be used to extract ordered sequences from the set.
- Linkedhashset: query speed with a hashset, and internal use of the linked list to maintain the order of elements (hash function + linked list ). When you use the iterator to traverse the set, the result is displayed in the order of element insertion.
5) map implementation class hashtable, hashmap and weakhashmap
- Hashtable is synchronous and not allowedNull.It inherits from the old dictionary class.
- Hashmap is non-synchronous and faster than hashtable.Null is allowed. Hashmap inherits from the abstractmap class. The internal hash array is different from that of hashtable. Hashmap is the best choice for inserting, deleting, and locating elements in a map.
- Weakhashmap is an improved hashmap. If a key is no longer referenced by external entities, it can be recycled by GC.
- TreemapAll elements in the table are in a fixed order. If you need ordered results, you should use treemap (the arrangement order of elements in the hashmap is not fixed ).
3. Conclusion
- Container objects can only hold object references (object pointers), rather than copying object information to the end of the series.
- In a multi-threaded environment, for non-synchronous integration and map, you can use a similarList list = collections. synchronizedlist (new collections list (...); orCollections. synchronizedmap (originmap)To achieve its synchronization, or other manual synchronization methods.
- Map provides not the association of objects and arrays, but the association of objects and objects.
- Set only accepts non-repeated objects. Hashset provides the fastest query speed. Treeset maintains the order of elements. Linkedhashset maintains the insert sequence of elements.
- There is no needCodeUse the vector, hashtable, and stack left behind by the old class library.