A collection is also called a container: from a large category into two categories: collection and map, i.e., single and double column lists.
A diagram in the Java programming concept illustrates the overall structure of the system: where black-focused classes are frequently used classes.
1 Collection
Collection: The root interface as a single-column collection. The inheritance system for this class of collections is as follows:
Collection is divided into two main categories: list and set
1) List:
features : Ordered collection (also known as sequences); Lists often allow duplicate elements.
The List interface provides a special iterator, called Listiterator, that allows element insertion and substitution, as well as bidirectional access, in addition to the normal operation that the Iterator interface provides.
Common sub-class implementations : Vector/arraylist/linkedlist
the difference between the three :
ArrayList: The underlying structure is an array, query fast, adding and deleting slow, implementation is not synchronized, thread insecure, high efficiency.
LinkedList: The bottom is linked list, query slow, delete quickly, realize the different steps, thread unsafe, high efficiency.
Vectors: The underlying data structures are arrays, fast queries, additions and deletions, synchronization, thread safety, low efficiency (even thread-safe, because the Collections Collection tool class provides a way to thread-safe a thread-insecure collection, This class is often replaced by the ArrayList collection)
2) Set:
features : unordered, duplicate elements are not allowed in the list (unordered: The order in which the collection is stored and removed from the collection is inconsistent)
Common sub-class implementations : HashSet and TreeSet
HashSet:
features :
The underlying implementation is not synchronous and threads are unsafe.
By looking at the source: HashMap The bottom layer is hashmap (that is, the underlying data structure is a hash table), just wrapped.
The bottom of the hash table structure relies on the hashcode () and Equals () methods to guarantee the uniqueness of the element.
The uniqueness of the implementation:
For the basic wrapper type: The wrapper class for the base type object has implemented the Equals () and Hashcode () methods, which guarantees the uniqueness of the element.
String class: Overrides the Equals () and Hashcode () methods, and can also guarantee uniqueness.
Custom classes: You need to override the Equals () method and the Hashcode () method in the object class
Its common subclasses:
Linkedhashset:
features: a hash table and a link list implementation of a Set interface with predictable iteration order. This implementation is non-synchronous.
This implementation differs from HashSet in that the latter maintains a double-link list that runs on all items.
This list of links defines the order of iterations, that is, the order in which the elements are inserted into the set (insert order).
Its data structure is a combination of a hash table and a linked list, both to ensure uniqueness and can be ordered. (hash table guarantees element uniqueness; Chain List guarantees order)
Again, the order of this place is consistent with the sequence of accesses ....
TreeSet:
Features:
The underlying implementation is non-synchronous and thread insecure.
Look at the source code, its bottom is packaged TreeMap collection, data structure is red black tree is balanced binary tree.
Balanced binary tree Features: Ability to sort elements, sort by: natural sort and comparator sort
The ordering here (where the position of the stored element is sorted) is not a concept, as it is ordered (in the same order of insertion and removal).
Implementation sort:
Natural Sorting : When you create a collection, you use a constructor that does not have a comparer, and you typically use a parameterless construct.
For a custom storage element object, to achieve a natural ordering, the Comparable<t> interface should be implemented to implement its only method: int compareTo (T o).
For the basic type of boxed object (such as Integer/character, etc.), because it has already implemented the interface, so directly can be used to achieve natural ordering.
String class: It has implemented the comparable interface, but also implemented its CompareTo method, so it can be directly accessed to achieve natural ordering.
Comparator Sort : If you want to use a comparator to sort, you need to use a parameter construct to pass in a comparer comparator subclass object into the constructor. There are generally two ways:
Inner class mode: When you create a collection object, you use an inner class to implement a comparer object, followed by an instance.
External class mode: Create a new class, implement the Comparator<t> interface, and implement the method in it: Int compare (T o1,t O2), creating an instance of the class before creating the collection, passing in the collection with the parameter constructs.
There are generally three ways to traverse a Colllection collection :
1) iterators
2) Enhanced for
3) General for
2 Map
Characteristics:
Store key-value pairs, depending on the key
A map cannot contain duplicate keys; Each key can only be mapped to one value (key unique, value repeatable)
Its implementation subclasses have the Allow null key and null value, and some do not allow null keys and null values
Common sub-categories :
HashMap TreeMap linkedhashmap Concurrenthashmap (more popular in multi-threaded) Hashtable (no longer used)
HashMap: A collection of arrays and linked lists (that is, a hash table) that relies on Key's hashcode () and the Equals () method of key
Inserting a key value is also a entry, first calculating the H=h (Key.hashcode ()) is the hashcode hash function of key, and then finding the corresponding index in the array based on the H value.
If the location already has elements, it needs to be compared with each element of the linked list at that index.
Hashtable:
Occurs in JDK1.0, earlier than the collection, after being modified to implement the map interface formally added to the collection.
This class implements a hash table, the underlying implementation is synchronous, so thread safety, but inefficient.
There are already hashmap replaced, generally not.
Concurrenthashmap:
Located under the Java.util.concurrent package, it is clearly used for multithreading.
Although the class is also synchronous, its lock density and hash table are different, its efficiency is higher than the Hashtable, of course, compared with hashmap efficiency has decreased, but in high concurrency, it guarantees security.
Its underlying synchronous source code implementation is also a major highlight.
The difference between HashMap and Hashtable:
HashMap is used to replace Hashtable. Except for the next two points, the rest is almost
1) Hashtable is synchronous, thread-safe, low-efficiency. HashMap threads are unsafe and highly efficient.
2) HashMap allows null keys and null values, while Hashtable does not allow NULL keys and null values
Traversal mode:
Values are searched by key:
1) Get the collection of keys:set<t> KeySet ()
2) Iterate through the set set of the key, looking for the corresponding value according to the key: V get (Object K)
Gets the collection of key-value pairs:
1) Get key value pair set set:set<map.entry<k,v>> EntrySet ()
2) traverse the set set: its element type is map.entry<k,v>, and each element type in the collection is map.entry<k,v>.
3) Obtain the keys and corresponding values according to the: K GetKey () and V GetValue () methods in this class.
Overview of the Java collection