JAVA Set Knowledge Points Summary <3> Map Set

Posted by mraiur on Fri, 31 May 2019 20:53:38 +0200

Map interface

Map is a special set, also known as mapping, which stores two data at a time (a key-value pair, Entry). Keys and values can be of any type, keys are not allowed to repeat, and values are allowed to repeat.
A key has and can only correspond to one value. The types of keys and values can be different.

Map

CHARACTERISTICS

Map collections store key-value pairs, where keys are unique.

The key of a Map set corresponds to a value, and the value does not require uniqueness, but a key cannot correspond to a value.

Map sets do not have Iterator iterators, and if iterations are needed, they need to be converted into indirect iterations of single-column sets.

III. Inheritance Relations

Map

| Hashtable: Hash table structure, no guarantee of access order, no null key or null value allowed, thread safety, low efficiency, has been replaced by HashMap

| Propertise: Key-value pairs are String-type Map collections, including methods for direct convection operations, specifically for configuration files.

| HashMap: Hash table structure, not guaranteeing access order, allowing null keys and null values, non-thread security, high efficiency.

| LinkedHashMap: Hash table structure with two-way linked list, keeping access order, allowing null keys and null values, non-thread-safe, efficient.

| TreeMap: A balanced sort binary tree (red-black tree) structure that stores elements in a natural sort or comparator to ensure that elements are ordered and non-thread-safe.
Element uniqueness depends on the ComparaTo method or Comparator comparator.

4. Common methods

Part of the method of a Map collection is similar to that of a Set collection.

  • Adding key-value pairs
V  put(K key, V value)
  • delete
void clear()
V remove(Object key)      Delete key-value pairs by key
  • judge
boolean containsKey(Object key)   First judge hashCode  Then judgeequals
boolean containsValue(Object value)    judgeequals
boolean isEmpty()
  • Obtain
V get(Object key)
int size()
Set<K>  keySet()
Collection<V>  values()
Set<Map.Entry<K,V>> entrySet() 

V. HashMap

Map implementation based on hash table structure allows null values and null keys to be used without guaranteeing sequence and thread safety.

Construction method

    public HashMap()//Construct an empty HashMap object with default initial capacity (16) and default load factor (0.75).

Other methods come from the Map interface

LinkedHashMap

Map implementation based on hash table and linked list structure allows null values and null keys to be used to maintain the insertion order as traversal order without ensuring thread safety.
Construction method

    public LinkedHashMap()//Construct an empty LinkedHashMap object with default initial capacity (16) and load factor (0.75).

Other methods come from the Map interface

TreeMap

Map implementation based on balanced binary tree structure does not allow null keys and null values. Sort according to the natural order of its keys. Thread security is not guaranteed.

Construction method:

public TreeMap(), which uses the natural order of keys to construct an empty object.

Own method:

public Map.Entry<K,V> ceilingEntry(K key)
public K ceilingKey(K key)
public Map.Entry<K,V> firstEntry()
public K firstKey()
public Map.Entry<K,V> lastEntry()
public K lastKey()
public Map.Entry<K,V> higherEntry(K key)
public K higherKey(K key)
public Map.Entry<K,V> lowerEntry(K key)
public K lowerKey(K key)

Iterator of Map

If you only traverse all keys, Set keySet()

If only all values are traversed, Collection values()

If you want to traverse in pairs, can you traverse the Map set directly with foreach or yesterday's iterator?
If you want to traverse in pairs, there are two ways:

  • Way 1: Get all the keys, and then find the value according to the key.

  • Mode 2: Map.Entry interface, which is used to get the set of key-value pairs

K getKey() returns the key corresponding to this item.
V getValue() returns the value corresponding to this item.

public class TestBianli {

    public static void main(String[] args) {
        HashMap<Integer, String> hm=new HashMap<>();
        hm.put(1, "a");
        hm.put(2, "b");
        hm.put(3, "c");
        hm.put(4, "d");
        hm.put(5, "e");

        //1. The Map collection does not implement the java.lang.Iterable interface
        //The inner part of the HashMap class creates a Set set Set class that can store key-value pairs, which can be obtained by using entrySet().

        //2. Need a data type that can store key-value pairs
        //The HashMap class built its own data type, Entry interface, to store key-value pairs.

        for (Entry<Integer, String> e : hm.entrySet()) {
            //3. I want to get keys or values separately.
            System.out.println(e.getKey());
            System.out.println(e.getValue());
        }

        //Change the above code to the original iterator code
        Set<Entry<Integer, String>>  entrys=hm.entrySet();
        Iterator<Entry<Integer, String>> it=entrys.iterator();
        while(it.hasNext()){
            Entry<Integer, String> e = it.next();
            System.out.println(e.getKey());
            System.out.println(e.getValue());
        }
    }
}

Project address: Portal

Topics: Java