API and usage of map collection in java

java.util.Map<k,v> collection


1. The characteristics
of the Map set 1. The Map set is a two-column set. One element contains two values, one key and one value.
2. The data types of key and value of the elements in the Map collection can be the same or different.
3. For the elements in the Map collection, the key is not allowed to be repeated, and the value can be repeated.
4. The key and value of the elements in the Map collection are in one-to-one correspondence.
2. The characteristics of the java.util.HashMap<k,v> collection implements Map<k,v> interface
HashMap collection:
(1) The bottom layer of the HashMap collection is a hash table, and the query speed is extremely fast.
(2) The HashMap collection is an unordered collection, and the order of storing elements and removing elements may be inconsistent.
Three, java.util.LinkedHashMap<k,v> collection extends HashMap<k,v>
The characteristics of the collection LinkedHashMap:
(1) The bottom layer of the LinkedHashMap collection is a hash table + a linked list.
(2) The LinkedHashMap collection is an ordered collection, and the order of storing elements and removing elements is the same.

Common methods in Map:

Delete all key-value pairs in the Map object;

void clear():

Query whether the specified key value is included in the Map ;

boolean containsKey(Object key):

Query whether the Map contains one or more values;

boolean containsValue(Object value):

Return the Set collection composed of the key-value pairs contained in the map. Each collection is a Map.Entry object.

Set entrySet():

Return the value corresponding to the specified key, or null if the key is not included;

get():

Query whether the Map is empty;

boolean isEmpty()

Return the collection of all keys in the Map;

Set keySet():

Returns the Collection composed of all values ​​in the Map.

Collection values()

Add a key-value pair, if the key in the set is duplicated, the original key-value pair will be overwritten;

Object put(Object key,Object value)

Copy the key-value pairs in the Map to this Map;

void putAll(Map m)

Delete the key-value pair corresponding to the specified key, and return the value of the deleted key-value pair, or null if it does not exist;

Object remove(Object key):

Delete the specified key-value pair, and return true if the deletion succeeds;
 

boolean remove(Object key,Object value):

Return the number of key-value pairs in the Map;

int size():

Inner class Entry

Map includes an internal class Entry, which encapsulates a key-value pair. Common methods are:

Object getKey(): return the key value contained in the Entry;
Object getvalue(): return the value contained in the Entry;
Object setValue(V value): set the value contained in the Entry and set the new value .

HashMap<String, Integer> hm = new HashMap<>();

//放入元素
 hm.put("Harry",23);
 hm.put("Jenny",24);
 hm.put("XiaoLi",20);

 System.out.println(hm);//{XiaoLi=20, Harry=23, Jenny=24}
 System.out.println(hm.keySet());//[XiaoLi, Harry, Jenny]
 System.out.println(hm.values());//[20, 23, 24]

 Set<Map.Entry<String, Integer>> entries = hm.entrySet();

 for (Map.Entry<String, Integer> entry : entries) {
     System.out.println(entry.getKey());
     System.out.println(entry.getValue());
        }

Java8 improved HashMap and Hashtable implementation classes

https://blog.csdn.net/mashaokang1314/article/details/83784159?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

Guess you like

Origin blog.csdn.net/qq_34159161/article/details/106871670