Previously, we have systematically studied the list. Next, we learn map and then set, because the implementation class of set is implemented based on map (for example, HashSet is implemented through HASHMAP, TreeSet is implemented through TreeMap).
First, let's look at the map schema.
As shown above:
A map is a mapping interface in which the content stored in a map is a key-value pair (key-value).
Abstractmap is an abstract class that inherits from the map and implements most of the APIs in the map. The implementation classes of other maps can reduce duplicate encoding by inheriting Abstractmap.
SortedMap is an interface that inherits from the map. The contents of the SortedMap are sorted key-value pairs, which are sorted by means of a comparer (Comparator).
Navigablemap is an interface that inherits from SortedMap. There are a series of navigation methods compared to sortedmap,navigablemap, such as "Get a key value pair that is greater than/equal to an object", "Get a key value pair that is less than/equal to an object", and so on.
TreeMap inherits from Abstractmap and implements the Navigablemap interface; Therefore, the content in TreeMap is "ordered key-value pairs"!
HashMap inherits from Abstractmap, but does not implement the Navigablemap interface; therefore, the HASHMAP content is "key value pair, but not guaranteed order"!
Hashtable is not inherited from Abstractmap, but it inherits from Dictionary (dictionary is also the interface of key-value pairs), and also implements the map interface; Therefore, the contents of Hashtable are "key-value pairs and do not guarantee order." But compared to HashMap, Hashtable is thread-safe, and it supports traversal through enumeration.
Weakhashmap inherited from Abstractmap. It differs from the HashMap key type, and the Weakhashmap key is "weak key."
With the overall framework above, let's look at the introduction to each interface and abstract class, and then explain each implementation class in detail.
1 Map
The map is defined as follows:
Public interface Map<k,v> {}
A map is a key-value pair (key-value) mapping interface. The map map cannot contain duplicate keys, and each key can be mapped to at most one value.
The map interface provides three collection views that allow you to view the contents of a map in the form of a keyset, a set of values, or a key-value mapping relationship.
Map mapping order. Some implementation classes can explicitly guarantee their order, such as TREEMAP, and other mapping implementations do not guarantee the order, such as the HashMap class.
The implementation class of the map should provide 2 "standard" constructs: the first, void (parameterless) construction method, which creates an empty map, and the second, a construction method with a single map type parameter that creates a new mapping with the same key-value mapping relationship as its parameters. In fact, the latter constructor allows the user to copy any mapping and generate an equivalent mapping of the desired class. Although this recommendation cannot be enforced (because the interface cannot contain a construction method), all common mapping implementations in the JDK follow it.
Map's API
abstract void Clear () Abstract Boolean containskey (Object key) Abstract Boolean Containsvalue (Object value) abstract set<entry<k, v>> EntrySet () Abstract Boolean equal S (object) abstract V get (object key) abstract int hashcode () Abstract Boolean IsEmpty () abstract set<k> keyset () abstract V put (K key, v value) abstr
ACT void Putall (map<. Extends K,? extends v> Map) abstract V Remove (Object key) abstract int size () abstract collection<v> values ()
Description
The map provides an interface for returning a keyset, a set of values, or a key-value mapping relationship, respectively.
EntrySet () is used to return a set set of key-value sets
Keyset () is used to return a set set of keyset
VALUES () user returns a collection collection of value sets
Because a map cannot contain duplicate keys, each key can be mapped to at most one value. Therefore, the key-value set, the keyset is set, and the value set is collection.
Map provides methods such as "key-value pairs", "get values based on keys", "Delete keys", "Get capacity Size", and so on.
2 Map.entry
The definition of Map.entry is as follows:
Interface Entry<k,v> {}
Map.entry is an interface within the map, Map.entry is a key-value pair, and Map obtains Map.entry's key-value pairs by EntrySet (), which enables the operation of key-value pairs through the collection.
API for Map.entry
Abstract Boolean equals (object)
Abstract K Getkey ()
Abstract V GetValue ()
abstract int hashcode ()
Abstract v SetValue (V object)
3 Abstractmap
The definition of Abstractmap is as follows:
Public abstract class Abstractmap<k,v> implements map<k,v> {}
The Abstractmap class provides the backbone implementation of the MAP interface to minimize the work required to implement this interface.
To implement a map that is not modifiable, programmers simply extend this class and provide an implementation of the EntrySet method, which returns the mapped set view of the mapping relationship. Typically, the returned set is implemented sequentially on the Abstractset. This set does not support the Add () or remove () method, nor does its iterator support the Remove () method.
To implement a modifiable mapping, the programmer must override the put method of this class (otherwise throw unsupportedoperationexception), EntrySet (). The iterator returned by iterator () must also implement its Remove method.
API for Abstractmap
Abstract set<entry<k, v>> entryset ()
void Clear ()
boolean containskey (Object key)
boolean Containsvalue (object value)
Boolean equals (object)
V get (object key)
int hashcode ()
boolean isempty ()
set<k> keyset ()
v put (K key, V Value)
void putall (map<? extends K,? extends v> Map)
V Remove (Object key)
int Size ()
String toString ()
collection<v> values ()
Object Clone ()
4 SortedMap
The definition of SortedMap is as follows:
Public interface sortedmap<k,v> extends map<k,v> {}
SortedMap is an interface that inherits from the map interface. It is an ordered SortedMap key value mapping.
SortedMap are sorted in two ways: natural sorting or a user-specified comparer. All elements that insert an ordered SORTEDMAP must implement the comparable interface (or be accepted by the specified comparer).
In addition, all SORTEDMAP implementation classes should provide 4 "standard" Construction methods:
(a) void (parameterless) construction method that creates an empty ordered map, sorted by the natural order of the keys.
(02) A construction method with a Comparator type parameter that creates an empty ordered map, sorted according to the specified comparer.
(03) A construction method with a map type parameter that creates a new ordered map with the same key-value mapping relation as the parameters, sorted according to the natural order of the keys.
(04) A construction method with a SortedMap type parameter that creates a new ordered map with the same key-value mapping and sorting method as the ordered mapping of the input. This recommendation cannot be guaranteed to be enforced because the interface cannot contain construction methods.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/