Map集合
1.Map集合和Collection集合的区别
Collection 是单身集合(单身汉)
Map 是双列集合(夫妻)
2、Map集合常用方法
public V put(K key,V value)
public V remove(K key)
public int size()
public V get(K key)
public boolean containsKey(K)
public boolean containsValue(V)
public Set<K> keySet()
public Set<Map.Entry<K,V>> entrySet()
3、Map集合的遍历方式
3.1、通过键,找到值
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "张三丰");
map.put(2, "周芷若");
map.put(3, "汪峰");
map.put(4, "灭绝师太");
Set<Integer> set = map.keySet();
for (Integer key : set) {
String value = map.get(key);
System.out.println(key + "=" + value);
}
3.2、Entry键值对对象(结婚证对象)
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "张三丰");
map.put(2, "周芷若");
map.put(3, "汪峰");
map.put(4, "灭绝师太");
for (Map.Entry<Integer, String> map1 : map.entrySet()) {
System.out.println(map1);
}
3.2.2、Entry键值对对象(结婚证对象)
Set<Map.Entry<String, String>> entry = map.entrySet();
for (Map.Entry<String, String> en : entry) {
System.out.println("丈夫:" + en.getKey() + ",媳妇:" + en.getValue());
}
4、JDK9的集合优化操作
A.集合工厂的方法 of(...) 方法
B.注意 a.三个父类的方法 Map List Set (父类才有效果)
b.返回的集合不可以改变