Java Collection series:-----------03MAP architecture

Source: Internet
Author: User
Tags delete key object object set set

We've learned about the collection framework before, and we'll look at the map schema. In front of us, we talked about the list combination in Conllection, not the set set in collection,

Because the set implementation classes in the collection framework are all based on map (for example, HashSet is implemented through HASHMAP, TreeSet is implemented by TreeMap).

The map schema is lateral to the collection architecture.

Such as:
Map is a map interface , and the content stored in the map is a key-value pair (key-value).
Abstractmap is an abstract class that inherits from map, which implements most of the APIs in the map. Other map implementations can reduce the number of duplicate encodings by inheriting Abstractmap.
SortedMap is an interface that inherits from map. The content in SortedMap is a sorted key-value pair , and the sort method is through the comparator (Comparator).
Navigablemap is an interface that inherits from SortedMap. There are a number of navigation methods compared to sortedmap,navigablemap, such as "Get a key value pair greater than/equal to an object," "Get a key value pair less than/equal to an object," and so on.
TreeMap inherits from Abstractmap and implements the Navigablemap interface, so the contents of TreeMap are " ordered key-value pairs "!
HashMap inherits from Abstractmap, but does not implement the Navigablemap interface; Therefore, the contents of HashMap are " key-value pairs, but not guaranteed order "!
(Hashtable) Although not inherited from Abstractmap, it inherits from Dictionary (dictionary is also an interface for key-value pairs), and also implements the map interface; Therefore, the contents of Hashtable are also " key-value pairs, and no guarantee of order ". However, compared to HashMap, Hashtable is thread-safe, and it supports traversal through enumeration.
(Weakhashmap) inherited from Abstractmap. It is different from the key type of HashMap, and thekey of Weakhashmap is "weak key".

before detailing each implementation class, take a look at the general description of each interface and abstract class. the content includes:
1 Map
2 Map.entry
3 Abstractmap
4 SortedMap
5 Navigablemap
6 Dictionary

1:map:

Definition of Map:

 Public Interface map<k,v> {}

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 , value set , or key-value mapping relationship set.
Map map Order. Some implementation classes can clearly guarantee their order, such as TreeMap, while others do not guarantee the order, such as the HashMap class.
The implementation class for MAP should provide 2 "standard" construction methods: The first, void (parameterless) construction method for creating 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 mappings as its arguments. in fact, the latter constructor allows the user to copy arbitrary mappings, generating an equivalent mapping of the required classes. Although this recommendation cannot be enforced (because an interface cannot contain a constructor method), all common mapping implementations in the JDK follow it.

Map's API

Abstract voidClear ()Abstract BooleanContainsKey (Object key)Abstract BooleanContainsvalue (Object value)AbstractSet<entry<k, v>>EntrySet ()Abstract Booleanequals (Object object)AbstractV get (Object key)Abstract inthashcode ()Abstract BooleanIsEmpty ()AbstractSet<k>KeySet ()Abstractv put (K key, v value)Abstract voidPutall (map<?extendsK?extendsV>map)AbstractV Remove (Object key)Abstract intsize ()AbstractCollection<v> VALUES ()

Description :
The map provides an interface for returning a keyset, a value set, or a key-value mapping relationship set, respectively.
EntrySet () The set collection for the return key-value set
KeySet () to return the set collection of keyset
VALUES () collection collection of user return value sets
Because the 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 pair, get value by key, delete key, get capacity size, and so on.

2:map.entry:

Map.entry is defined as follows:

Interface entry<k,v> {}

Map.entry is an internal interface in the map, Map.entry is a key-value pair , and Map obtains the Map.entry key-value pair collection through the EntrySet (), thereby implementing the operation of the key-value pair through the collection.

Our article is about the entry and HashMap inside the entry is not the same, hashmap inside the entry inherited the entry here, is its sub-class.

Map.entry's API

Abstract Boolean      equals (Object object)abstract  K             getKey ()abstract  V             getValue ( )abstractint         hashcode ()abstract v             setValue (v object)

3; Abstractmap:

Abstractmap is defined as follows:

 Public Abstract class Implements map<k,v> {}

The Abstractmap class provides a backbone implementation of the MAP interface to minimize the work required to implement this interface.
To implement a non-modifiable mapping, the programmer simply extends this class and provides the implementation of the EntrySet method, which returns a mapped set view of mappings. Typically, the returned set is implemented sequentially on Abstractset. This set does not support the Add () or remove () methods, and its iterators do not support the Remove () method.

To implement a modifiable mapping, the programmer must override the put method of this class (otherwise it will throw unsupportedoperationexception), and EntrySet (). iterator () The returned iterator must also implement its Remove method in addition.

Abstractmap's API

AbstractSet<entry<k, v>>EntrySet ()voidClear ()BooleanContainsKey (Object key)BooleanContainsvalue (Object value)Booleanequals (Object object) V get (object key)inthashcode ()BooleanisEmpty () Set<K>KeySet () v Put (K key, v value)voidPutall (map<?extendsK?extendsV>map) V Remove (Object key)intsize () String toString () Collection<V>values () Object clone ()

SortedMap:

SortedMap is defined as follows:

 Public Interface extends map<k,v> {}

SortedMap is an interface that inherits from the map interface. It is an ordered SortedMap key-value mapping.
There are two ways to sort SortedMap: natural sorting or user-specified comparators . 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:
void (no argument) constructs a method that creates an empty ordered map, sorted by the natural order of the keys.
a constructor with a Comparator type parameter that creates an empty ordered map that is sorted according to the specified comparer.
a constructor with a map type parameter that creates a new ordered map with key-value mappings that are the same as the parameters, sorted by the natural order of the keys.
a construction method with a SortedMap type parameter that creates a new ordered map with key-value mappings and ordering methods that are the same as the ordered mappings for the input. It is not guaranteed to enforce this recommendation because an interface cannot contain a constructor method.

SortedMap's API

//API that inherits from mapAbstract voidClear ()Abstract BooleanContainsKey (Object key)Abstract BooleanContainsvalue (Object value)AbstractSet<entry<k, v>>EntrySet ()Abstract Booleanequals (Object object)AbstractV get (Object key)Abstract inthashcode ()Abstract BooleanIsEmpty ()AbstractSet<k>KeySet ()Abstractv put (K key, v value)Abstract voidPutall (map<?extendsK?extendsV>map)AbstractV Remove (Object key)Abstract intsize ()AbstractCollection<v>values ()//SortedMap the new APIAbstractcomparator<?SuperK>Comparator ()AbstractK Firstkey ()AbstractSortedmap<k, v>Headmap (K endkey)AbstractK Lastkey ()AbstractSortedmap<k, v>SubMap (k Startkey, K EndKey)AbstractSortedmap<k, v> tailmap (K startkey)

5:navigablemap

Navigablemap is defined as follows:

 Public Interface 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 to a given search target.
Navigablemap provides methods for getting "key", "key-value pair", "keyset", "key-value pair set" respectively.

Navigablemap's API

AbstractEntry<k, v>ceilingentry (K key)AbstractEntry<k, v>firstentry ()AbstractEntry<k, v>floorentry (K key)AbstractEntry<k, v>higherentry (K key)AbstractEntry<k, v>lastentry ()AbstractEntry<k, v>lowerentry (K key)AbstractEntry<k, v>pollfirstentry ()AbstractEntry<k, v>polllastentry ()Abstractk Ceilingkey (k key)Abstractk Floorkey (k key)Abstractk Higherkey (k key)Abstractk Lowerkey (k key)AbstractNavigableset<k>Descendingkeyset ()AbstractNavigableset<k>Navigablekeyset ()AbstractNavigablemap<k, v>Descendingmap ()AbstractNavigablemap<k, v> headmap (K Tokey,Booleaninclusive)AbstractSortedmap<k, v>Headmap (K tokey)AbstractSortedmap<k, v>SubMap (k Fromkey, K Tokey)AbstractNavigablemap<k, v> subMap (K Fromkey,BooleanFrominclusive, K Tokey,Booleantoinclusive)AbstractSortedmap<k, v>Tailmap (K fromkey)AbstractNavigablemap<k, v> tailmap (K Fromkey,BooleanInclusive

Description :

In addition to inheriting the characteristics of SortedMap, NAVIGABLEMAP provides functions that can be divided into 4 categories:
Class 1th, which provides methods for manipulating key-value pairs.
Lowerentry, Floorentry, Ceilingentry, and Higherentry methods, which return the Map.entry object associated with a key that is less than, less than or equal to, greater than or equal to, greater than the given key.
The Firstentry, Pollfirstentry, Lastentry, and Polllastentry methods, which return and/or remove the minimum and maximum mapping relationships (if present), otherwise return null.
Class 2nd, which provides methods for manipulating keys . This is similar to the 1th kind of comparison.
Lowerkey, Floorkey, Ceilingkey, and Higherkey methods, which return keys that are less than, less than or equal to, greater than or equal to, and greater than the given key.
Class 3rd, gets the keyset.
Navigablekeyset, Descendingkeyset get the key set of the positive sequence/reverse order respectively.
Class 4th, gets a subset of key-value pairs.

Java Collection series:-----------03MAP architecture

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.