JAVA Map architecture and API Introduction _java

Source: Internet
Author: User
Tags int size object object set set

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:

Copy Code code 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
Copy Code code as follows:

abstract void Clear ()
Abstract Boolean ContainsKey (Object key)
Abstract Boolean Containsvalue (Object value)
Abstract set<entry<k, v>> EntrySet ()
Abstract Boolean equals (object)
Abstract V get (Object key)
abstract int hashcode ()
Abstract Boolean IsEmpty ()
Abstract set<k> keyset ()
Abstract v put (K key, V value)
abstract 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:

Copy Code code 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
Copy Code code as follows:

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:
Copy Code code 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
Copy Code code as follows:

Abstract set<entry<k, v>> EntrySet ()
void Clear ()
Boolean ContainsKey (Object key)
Boolean Containsvalue (Object value)
Boolean equals (Object 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:
Copy Code code 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.

API for SortedMap
Copy Code code as follows:

The API that inherits from the map
abstract void Clear ()
Abstract Boolean ContainsKey (Object key)
Abstract Boolean Containsvalue (Object value)
Abstract set<entry<k, v>> EntrySet ()
Abstract Boolean equals (object)
Abstract V get (Object key)
abstract int hashcode ()
Abstract Boolean IsEmpty ()
Abstract set<k> keyset ()
Abstract v put (K key, V value)
abstract void Putall (map<? extends K,? extends V> Map)
Abstract V Remove (Object key)
abstract int size ()
Abstract collection<v> VALUES ()
SortedMap the new API
Abstract comparator<? Super K> Comparator ()
Abstract K Firstkey ()
Abstract sortedmap<k, v> headmap (K endkey)
Abstract K Lastkey ()
Abstract sortedmap<k, v> SubMap (k Startkey, K EndKey)
Abstract sortedmap<k, v> tailmap (K startkey)


5 Navigablemap
The definition of Navigablemap is as follows:
Public interface navigablemap<k,v> extends sortedmap<k,v> {}
Navigablemap is an interface that inherits from SortedMap. It is a navigable set of key-value pairs with a navigation method that reports the closest match for a given search target.
Navigablemap provides methods for getting "keys", "key-value pairs", "keyset", "key-value pair" sets, respectively.
API for Navigablemap
Copy Code code as follows:

Abstract entry<k, v> ceilingentry (K key)
Abstract entry<k, v> firstentry ()
Abstract entry<k, v> floorentry (K key)
Abstract entry<k, v> higherentry (K key)
Abstract entry<k, v> lastentry ()
Abstract entry<k, v> lowerentry (K key)
Abstract entry<k, v> pollfirstentry ()
Abstract entry<k, v> polllastentry ()
Abstract k Ceilingkey (k key)
Abstract k Floorkey (k key)
Abstract k Higherkey (k key)
Abstract k Lowerkey (k key)
Abstract navigableset<k> Descendingkeyset ()
Abstract navigableset<k> Navigablekeyset ()
Abstract navigablemap<k, v> Descendingmap ()
Abstract navigablemap<k, v> headmap (K Tokey, Boolean inclusive)
Abstract sortedmap<k, v> headmap (K tokey)
Abstract sortedmap<k, v> SubMap (k Fromkey, K Tokey)
Abstract navigablemap<k, v> SubMap (K Fromkey, Boolean frominclusive, K Tokey, Boolean toinclusive)
Abstract sortedmap<k, v> tailmap (K fromkey)
Abstract navigablemap<k, v> tailmap (K Fromkey, Boolean inclusive)

Description
In addition to inheriting SortedMap's features, Navigablemap can be divided into 4 categories:
The 1th class provides a method for manipulating key-value pairs.
Lowerentry, Floorentry, Ceilingentry, and Higherentry methods that return the Map.entry object that is associated with a key less than, less than, equal to, or greater than or equal to the given key.
Firstentry, Pollfirstentry, Lastentry, and Polllastentry methods that return and/or remove the smallest and largest mapping relationships (if they exist), or null.
The 2nd class provides a way to manipulate the keys. This is similar to class 1th.
Lowerkey, Floorkey, Ceilingkey, and Higherkey methods that return keys that are less than, less than, equal to, greater than, or greater than the given key.
Class 3rd, get the keyset.
Navigablekeyset and Descendingkeyset get the key sets of positive sequence/inverse sequence respectively.
The 4th class, gets a subset of the key-value pairs.

6 Dictionary
The definition of dictionary is as follows:

Copy Code code as follows:

Public abstract class Dictionary<k,v> {}

Navigablemap is the interface to the key-value pairs defined by JDK 1.0, which also includes the basic functions of manipulating key-value pairs.
API for Dictionary
Copy Code code as follows:

Abstract enumeration<v> elements ()
Abstract V get (Object key)
Abstract Boolean IsEmpty ()
Abstract enumeration<k> keys ()
Abstract v put (K key, V value)
Abstract V Remove (Object key)
abstract int

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.