HashMap
HashMap是集合Map接口的实现类,底层数据结构是数组+链表
Map是处理键值对数据的
概念
左边的一列称为键,右边的一列称为值
键唯一,值不唯一,键值一一对应
一对键值称为键值对或键值对对象,又称Entry对象
体系
Map
Map是双列集合的顶层接口,它的功能是全部双列集合都可以继承使用的
Map是一个接口,创建对象是不能直接创建,要创建它的实现类的对象;后面的泛型<K,V>中K是键,V是值
添加数据put
package Map;
import java.util.HashMap;
public class demo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("zhangsan","1");
map.put("lisi","2");
map.put("wangwu","3");
System.out.println(map);//{lisi=2, zhangsan=1, wangwu=3}
}
}
HashMap也是无序、不重复的
HashMap是根据数据的key来得出数据存放位置的,当两个数据的key都是A时,新的A会覆盖旧的A
package Map;
import java.util.HashMap;
public class demo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("zhangsan","1");
map.put("lisi","2");
map.put("wangwu","3");
System.out.println(map);//{lisi=2, zhangsan=1, wangwu=3}
map.put("zhangsan","4");
System.out.println(map);//{lisi=2, zhangsan=4, wangwu=3}
}
}
putIfAbsent
这种方法也能添加数据,不过当添加数据的key已经存在时,它能执行但不会添加进去
如zhaoliu已经有了,就不会被覆盖掉,value还是4
package Map;
import java.util.HashMap;
public class demo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("zhangsan","1");
map.put("lisi","2");
map.put("wangwu","3");
System.out.println(map);//{lisi=2, zhangsan=1, wangwu=3}
map.putIfAbsent("zhaoliu","4");
System.out.println(map);//{lisi=2, zhaoliu=4, zhangsan=1, wangwu=3}
map.putIfAbsent("zhaoliu","5");
System.out.println(map);//{lisi=2, zhaoliu=4, zhangsan=1, wangwu=3}
}
}
删除remove
package Map;
import java.util.HashMap;
public class demo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("zhangsan","1");
map.put("lisi","2");
map.put("wangwu","3");
System.out.println(map);//{lisi=2, zhangsan=1, wangwu=3}
map.remove("zhangsan");
System.out.println(map);//{lisi=2, wangwu=3}
/*map.remove("lisi","5");
这种写法是当key和value都对应上才能删除,lisi的value是2,所以不会删除
*/
}
}
修改replace
package Map;
import java.util.HashMap;
public class demo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("zhangsan","1");
map.put("lisi","2");
map.put("wangwu","3");
System.out.println(map);//{lisi=2, zhangsan=1, wangwu=3}
System.out.println(map.replace("zhangsan", "4"));//1
System.out.println(map);//{lisi=2, zhangsan=4, wangwu=3}
}
}
put的修改效果
key相同时put方法会覆盖掉原来的数据,以此达到修改的效果
package Map;
import java.util.HashMap;
public class demo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("zhangsan","1");
map.put("lisi","2");
map.put("wangwu","3");
System.out.println(map);//{lisi=2, zhangsan=1, wangwu=3}
System.out.println(map.put("zhangsan","4"));//返回被修改的数据的value 1
System.out.println(map);//{lisi=2, zhangsan=4, wangwu=3}
}
}
查询get
package Map;
import java.util.HashMap;
public class demo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("zhangsan","1");
map.put("lisi","2");
map.put("wangwu","3");
System.out.println(map);//{lisi=2, zhangsan=1, wangwu=3}
System.out.println(map.get("zhangsan"));//get返回key对应的value
}
}
常用方法
与LinkedList不同,HashMap的链表是单向的,所以查询时会比较慢
clear
map.clear();
keySet
把所有的key放进一个集合中
package Map;
import java.util.HashMap;
public class demo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("zhangsan","1");
map.put("lisi","2");
map.put("wangwu","3");
System.out.println(map);//{lisi=2, zhangsan=1, wangwu=3}
System.out.println(map.keySet());//[lisi, zhangsan, wangwu]
}
}
values
把所有的value放进一个集合中
package Map;
import java.util.HashMap;
public class demo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("zhangsan","1");
map.put("lisi","2");
map.put("wangwu","3");
System.out.println(map);//{lisi=2, zhangsan=1, wangwu=3}
System.out.println(map.values());//[2, 1, 3]
}
}
contains
package Map;
import java.util.HashMap;
public class demo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("zhangsan","1");
map.put("lisi","2");
map.put("wangwu","3");
System.out.println(map);//{lisi=2, zhangsan=1, wangwu=3}
System.out.println(map.containsKey("zhangsan"));//true
System.out.println(map.containsValue("1"));//true
}
}
获取键值对对象entrySet
package Map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class demo {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("zhangsan","1");
map.put("lisi","2");
map.put("wangwu","3");
System.out.println(map);//{lisi=2, zhangsan=1, wangwu=3}
//map.entrySet().var回车后就能得到下面的代码
Set<Map.Entry<String, String>> entries = map.entrySet();//Map.Entry是Map内部的接口
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry);
/*
lisi=2
zhangsan=1
wangwu=3
*/
}
}
}
其他还有size、isEmpty、clone等方法