Java collection package -- Map interface and its sub interfaces, abstract classes

Please indicate the original address for Reprint: https://www.cnblogs.com/ygj0930/p/13565258.html

 

1: Macro structure

The disassembly memory is as follows:

1. Interface: the Map interface is the top-level interface. The stored content is key value pairs, which defines a series of mapping operation methods and specifications;

SortedMap inherits from the interface of Map, adds sorting attribute to the contact of top-level Map interface, stores the sorted key value pairs, and defines the sorting method through comparator;

NavigableMap inherits SortedMap's interface and provides a series of conditional query methods based on SortedMap's sorting key value pairs, such as "obtain key value pairs greater than / equal to an object", "obtain key value pairs less than / equal to an object", etc;

2. Abstract class: the AbstractMap abstract class implements the Map interface, which implements most of the API s in the Map;

Dictionary abstract class is another set of key value pair specifications defined in the old version. It is independent of the Map interface and is outdated.

3. Implementation class: HashMap inherits AbstractMap and defines some other methods based on the implemented methods of AbstractMap; [key value pairs are out of order]

While inheriting AbstractMap, TreeMap also implements the interface of NavigableMap, so it has the attribute of sorting and a series of additional conditional query methods;

WeakHashMap inherits AbstractMap. Its difference from HashMap is that its key type is different. The key of WeakHashMap is "weak key", and the value mapped by its key can be recycled by GC. At this time, the key value pair will be deleted automatically.

HashTable does not inherit AbstractMap. It inherits the abstract Dictionary class to save key value pairs. At the same time, it implements the Map interface and implements a unified key value pair operation method according to the specification of the Map interface. When it is different from HashMap, it is thread safe and outdated. Switch to ConcurrentHashMap.

 

2: Map interface

Map interface is the top-level interface of mapping, which highly abstracts the mapping and defines a series of general operation method specifications.

abstract void                 clear()
abstract boolean              containsKey(Object key)
abstract boolean              containsValue(Object value)
abstract Set<Entry<K, V>>     entrySet()
abstract boolean              equals(Object 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()

1) Map is a key value mapping interface. The Map cannot contain duplicate keys; Each key can be mapped to at most one value.

2) the Map interface provides three data views, allowing you to view the contents of a Map in the form of key set, value set or key value Map set.

entrySet() is used to return a Set of key value sets
keySet() returns the Set of keysets
values() the Collection of values returned by the user

 

The Map interface also defines an internal interface {entry < K, V >, which defines a series of operation specification methods of key value pairs:

abstract boolean     equals(Object object)
abstract K             getKey()
abstract V             getValue()
abstract int         hashCode()
abstract V             setValue(V object)

 

2: SortedMap interface

SortedMap interface inherits from the Map interface and adds the sorting attribute of key value pairs.

SortedMap can be sorted in two ways: natural sorting or {user specified comparator sorting.

All elements inserted into the ordered SortedMap must first implement the Comparable interface and the comparison method.

// Inherit from Map of API
abstract void                 clear()
abstract boolean              containsKey(Object key)
abstract boolean              containsValue(Object value)
abstract Set<Entry<K, V>>     entrySet()
abstract boolean              equals(Object 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 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)

 

3: NavigableMap interface

The NavigableMap interface inherits from the SortedMap interface to provide navigation methods for specific search targets based on the ordered key value pairs of SortedMap.

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)

Class 1, a method for finding a specific key value pair.
lowerEntry, floorEntry, ceilingEntry, and higherEntry methods that return maps associated with keys less than, less than or equal to, greater than or equal to, and greater than a given key, respectively Entry object.
firstEntry, pollFirstEntry, lastEntry, and pollLastEntry methods, which return and / or remove the minimum and maximum mapping relationships (if any), otherwise return null.
Class 2, methods for finding specific keys.
lowerKey, floorKey, ceilingKey and higherKey methods, which return keys less than, less than or equal to, greater than or equal to, and greater than the given key respectively.
The third type is the method of finding a specific} key set.
navigableKeySet and descendingKeySet obtain positive / negative order keysets respectively.
Class 4, get a subset of key value pairs.

 

4: AbstractMap abstract class

public abstract class AbstractMap<K,V> implements Map<K,V> {}

AbstractMap implements the Map interface and defines most general methods in the interface, reducing the coding of subsequent implementation classes.

      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()

 

5: Dictionary abstract class

public abstract class Dictionary<K,V> {}

It is an abstract class of key value pairs defined in the old version, which is independent of the Map interface. It also defines a series of key value pair operation function specifications, which is outdated at present.

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                size()

Its main implementation class is HashTable, which is also outdated.

  

Posted by ATLien on Fri, 20 May 2022 07:04:45 +0300