Map集合、HashMap、LinkedHashMap、Hashtable

1.Map

(1 Overview

In real life, we often see such a collection: the
one-to-one correspondence between IP address and host name, ID number and individual, system user name and system user object, etc. , is called mapping. Java provides a special collection class to store the objects of this object relationship, namely the java.util.Map
interface.
public interface Map<K,v>

The characteristics of the java. util.Map<k, v> collection
Map collection: 1. The Map collection
is a two-column collection, an element contains two values ​​(a key, a value)
2. The elements in the Map collection, key and value The data types can be the same or different
3. Elements in the Map collection, the key is not allowed to be repeated, and the value can be repeated
4. The elements in the Map collection, the key and value are one-to-one correspondence

The difference between map and collection In the collection of
the collection, the elements exist in isolation (understood as a singleton), and the elements stored in the collection are stored one by one.
In the collection of Map, the elements exist in pairs (understood as husband and wife). Each element is composed of two parts: key and value, and the corresponding value can be found through the key.
The collection in Collection is called single-column collection, and the collection in Map is called double-column collection.
It should be noted that the set in the Map cannot contain duplicate keys, and the values ​​can be duplicated; each key can only correspond to one value.
Map commonly used subclasses

HashMap<K,V> : The structure of the hash table used to store data, the access sequence of elements cannot be guaranteed to be consistent. To ensure the uniqueness and non-
duplication of the key, the hashCode() method and equals() method of the key need to be rewritten.
LinkedHashMap <K,V>: There is a subclass LinkedHashMap under HashMap, which uses hash table structure + linked list structure to store data.
The linked list structure can ensure that the access sequence of the elements is consistent; through the hash table structure, the uniqueness and non-repetition of the key can be guaranteed, and the
hashCode() method and equal) method of the key need to be rewritten .

Common methods in Map

1.public v put(K key, v value); Add the specified key and specified value to the Map collection.

      When storing key-value pairs, the key is not repeated and the return value v is null. When
      storing key-value pairs, the key is repeated, and the new value will be used to replace the repeated value in the map, and the replaced value value will be returned.

      Map<String,String> map=new HashMap<>;

      map.put("Yang Guo","Little Dragon Girl");

      System.out.println(map);

      Running result: {Yang Guo=Little Dragon Girl}

2. public V remove(object key): Delete the key-value pair element corresponding to the specified key in the Map collection, and return the value of the deleted element.

      Return value; V
      key exists, v returns the deleted value. key does not exist, v returns null

      //Create a Map collection object
      Map<String, Integer> map = new HashMap<>();
      map. put( "
      赵丽颖",168); map.put("杨颖",165);
      map. put("林志玲”,178);
      System. out. println(map); //{林志玲=178,赵丽颖=168, 杨颖=165}
      Integer v1 = map. remove( key: "林志玲");
      System.out. Println( "v1:" +v1); //v1:178
      System. out. println(map); //{赵丽颖-168,杨颖=165}|

3. Public V get (Object key) obtains the corresponding value in the Map collection according to the specified key.

     If the key exists, return the corresponding value, if the key does not exist, return null     

      //Create a Map collection object
      Map<String, Integer> map = new HashMap<>();
      map. put("Zhao Liying",168);
      map.put("杨颖" ,165);
      map. put("林志玲”,178);
      Integer v1 = map.get("杨颖");
      System.out. println("v1: "+v1); //v1:165
      Integer v2 = map. get("Di Lireba" );
      System.out. Println("v2: "+v2); //v2:null

4. boolean containsKey (Object key) determines whether the set contains the specified key.

      Return true if included, false if not included

      //Create a Map collection object
      Map<String, Integer> map = new HashMap<>( );
      map. put( "赵丽颖" ,168);
      map. put("杨颖" ,165);
      map. put("林志玲",178);
      boolean b1 = map. containsKey('赵丽颖");
      System. out. println("b1:"+b1); //b1: true
      boolean b2 = map. containsKey( "张无芝");
      System. out. println( "b2:"+b2); //b2:false

5.public Set<K> keySet(): Get all the keys in the Map collection and store them in the Set collection.

      The first traversal method of the Map collection: find the value through the key. The method in the
      Map collection:
      Set<K> keySet() returns a Set view of the keys contained in this map.
      Implementation steps:
      1. Use the method keySet() in the Map collection to take out all the keys of the Map collection and store them in a Set collection
      2. Traverse the set collection to obtain each key in the
      Map collection 3. Pass the Map collection The method get(key), find the value by key

6. public Set<Map. Entry<K,V>> entrySet(): Get the collection of all key-value pairs in the Map collection (Set collection).
We already know, the Map is stored in two kinds of objects, called a key (key), called value (value). They should be kept in pairs in the Map
Department, also known as the object to do Map One of the Entry (item). Entry encapsulates the correspondence of key-value pairs into objects. That is, the key-value pair object, so
when we traverse the Map collection, we can get the corresponding key and corresponding value from each key-value pair (Entry) object.
Since Entry represents a pair of keys and values, it also provides methods to obtain corresponding keys and corresponding values:
public K getKey()]: Get the key in the Entry object.
public V getValue(): Get the value in the Entry object.
The Map collection also provides methods to obtain all Entry objects:
public Set<Map. Entry<K,V>> entrySet(): Get the set of all key-value pairs in the Map collection (Set collection).

The second traversal of the Map collection

The second way to traverse the
Map collection : Use the Entry object to traverse the method in the Map collection:
Set<Map. Entry<K, V>> entrySet() returns the Set view of the mapping relationship contained in this map.
Implementation steps:
1. Use the entrySet() method in the Map collection to take out multiple Entry objects in the Map collection and store them in a Set collection
2. Traverse the Set collection to obtain each Entry object
3. Use the entry set in the Entry object Methods getKey() and getVolue() get key and value

Use for each to traverse

2.HashMap 

java. util. HashMap<k, v> collection implements Map<k, v> interface
HashMap collection characteristics:
1. The bottom layer of


HashMap collection is a hash table: query speed is particularly fast Before JDK 1.8: array + singly linked list After JDK1.8: Array + singly linked list/red-black tree (the length of the linked list exceeds 8): Improve the speed of the query 2. The hashMap collection is an unordered collection, and the order of storing elements and removing elements may be inconsistent.
HashMap is stored from Definition type (custom type as value)

HashMap stores custom types (custom type as key)

HashMap stores custom type key value
key: Person type
Person class must rewrite hashCode method and equals method to ensure that the key is unique
value: String type
can be repeated

3.LinkedHashMap

java. util. inkedHashMap<k, v> collection extends HashMap<k, v> The characteristics of the collection
LinkedHashMap:
1. The bottom layer of the L inkedHashMap collection is a hash table + a linked list (the order of iteration is guaranteed)
2. The L inkedHashMap collection is an ordered The order of storing elements and removing elements is the same. The
underlying principle:
hash table + linked list (recording the order of elements)

Keys are not allowed to be repeated, in order

4. Hashtable collection (replaced by HashMap)

java. util. Hashtable<K, V> collection implements Mop<K, V> interface
Hashtable: The bottom layer is also a hash table, which is a thread-safe collection, a single-threaded collection, and is slow.
HashMop: The bottom layer is a hash table The Greek table is a thread-unsafe collection, a multi-threaded collection, and a fast
HashMap collection (all collections learned before): can store null values, null key
Hashtable collections, cannot store null values, null keys
Hashtable and Vector The collection is also replaced by more advanced collections (HashMap, Arraylist) after the jdk1.2 version
. The subclass of Hashtable. Properties is still active on the stage of history. The
Properties collection is the only collection that combines with the I0 stream.


 

Guess you like

Origin blog.csdn.net/weixin_51980491/article/details/112968825