Container-Map interface, HashMap (15)
-
Map interface: Defines the storage characteristics of the double-case set pair , it is not a sub-interface of the Collection interface. The storage characteristics of the double-case set are stored in key and value structures , which embodies the concept of y=f(x) in mathematics.
-
The difference between Map and Collection:
- In the container in the Collection, the elements exist in isolation (understood as a singleton), and the elements stored in the collection are stored in the form of one element.
- The container in the Map, the elements exist in pairs (understood as a couple in modern society), each element is composed of two parts, a key and a value, and the corresponding value can be found through the key.
- The container in the Collection is called a singleton set, and the container in the Map is called a doubleton set
- The collection in the Map cannot contain duplicate keys, and the values can be duplicated : each key can only correspond to one value.
- The commonly used containers in Map are HashMap, TreeMap, etc.
-
Some common methods of HashMap :
-
The concrete realization of some common methods of HashMap
-
Add element
import java.util.HashMap; import java.util.Map; public class HashMapTest { public static void main(String[] args) { //实例化HashMap容器 Map<String,String> map=new HashMap<>(); //添加容器 map.put("a","A");//用put(K,V)添加元素,并且返回V的值,V为空时,返回的是null, String value=map.put("a","B");//K(键)相同时,新的V值会覆盖旧的V,返回的是被覆盖的旧的V System.out.println(value); } }
-
The first way to get the element is to get the element through the get method .
//通过K获取value的方法 String val = map.get("a");//缺点:1.当HashMap有很多元素时,我们需要调用多次get方法 System.out.println(val);// 2.假如我们不知道有哪些K,通过get方法就无法获取 //优点:1.通过已知的Key来获取Value,get方法是很方便的
-
The second method of obtaining elements, obtaining elements through the KeySet method , can solve the two shortcomings of the get method.
map.put("b","B"); map.put("c","D"); map.put("d","D"); map.put("e","E"); //获取HashMap容器中有的元素,可以使用KeySet方法与get方法一并完成 Set<String> keys=map.keySet();//KeySet方法获取所有的Key,放在一个Set集合里 for (String key:keys){ //遍历所有的Key String v1=map.get(key);//通过get方法是通过每个Key,去获取Value System.out.println(key+"----"+v1);//在Map接口中,Key一定要不同,Value可以相同 }
-
The third way to get elements is to get the Map.Entry type to get the element through the entrySet method .
//这个entrySet方法可以返回一个Set集合,Set所存放的类型是Map.Entry类型 //Map.Entry类型是:Entry接口继承Map,所以Entry是内部接口,Entry内部接口提供了两个方法,分别是getKey()、getValue() 获取K和V的值。Map外部接口调用Entry是内部接口的方法就要用Map.Entry //Map本身就是K-V的结构,而Entry类型就是一个K-V,entrySet方法返回的Set集合里就是存取多个K-V(键值对) Set<Map.Entry<String,String>> entrySet=map.entrySet(); for (Map.Entry<String,String> entry:entrySet){ String key=entry.getKey(); String v=entry.getValue(); System.out.println(key+"------------------"+v); }
-
Union operation
//并集操作 System.out.println("------------------------------"); Map<String,String> map2=new HashMap<>(); map2.put("f","F"); map2.put("c","cc");//当Key相同,value不同时,被并集的集合中的V,会覆盖并集的集合中的V map2.putAll(map);//map2去并集map //通过第二种keySet()方法获取元素 Set<String> keys2=map2.keySet(); for (String key:keys2){ System.out.println("Key: "+key+" Value: "+map2.get(key)); }
-
Delete element
//删除元素 System.out.println("-------------------------------"); String v=map.remove("e");//remove()通过Key删除Value值 System.out.println(v);//返回删除的Value值 Set<String> keys3=map.keySet(); for (String key:keys3){ System.out.println("Key: "+key+" Value "+map.get(key)); }
-
Determine whether the key exists
boolean flag = map.containsKey("a"); System.out.println(flag);
-
Determine whether Value exists
boolean flag2 = map.containsValue("BB"); System.out.println(flag2);
-