Map集合
• Map集合称为双列集合,格式为:
{key1 = value1, key2 = value2, key3 = value3, …}
一次需要保存一对数据作为一个元素
• Map集合的每个元素"key = value"称为一个键值对/键值对对象/Entry对象,Map集合也被叫做“键值对集合”
• Map集合的所有键不允许重复,但值可以重复,键和值是一一对应的,每一个键只能找到自己对应的值
注: Map系列集合的特点都是由键决定的,值只是一个附属品,值是不做要求的
• HashMap(由键决定特点): 无序、不重复、无索引(用得最多)
• LinkedHashMap(由键决定特点): 有序、不重复、无索引
• TreeMap(由键决定特点):按照大小默认升序排序、不重复、无索引
public class MapTest {
public static void main(String[] args) {
//HasMap:无序、不重复,无索引
Map<String, Integer> map = new HashMap<>();
System.out.println("------HashMap------");
map.put("手环", 168);
//后面重复的数据会覆盖前面的数据
map.put("手环", 170);
map.put("手机", 6998);
map.put("电脑", 12888);
map.put(null, null);
//{null=null, 电脑=12888, 手机=6998, 手环=170}
System.out.println(map);
System.out.println("------LinkedHashMap------");
//LinkedHashMap:有序、不重复、无索引
Map<String, Integer> map1 = new LinkedHashMap<>();
map1.put("手环", 168);
//后面重复的数据会覆盖前面的数据
map1.put("手环", 170);
map1.put("手机", 6998);
map1.put("电脑", 12888);
map1.put(null, null);
System.out.println(map1);
System.out.println("------TreeMap------");
//按照大小默认升序排序,不重复,无索引
Map<Integer, String> map2 = new TreeMap<>();
map2.put(5, "Python");
map2.put(4, "Java");
map2.put(2, "C");
map2.put(3, "Rust");
map2.put(1, "C++");
map2.put(5, "HTML5");
//后面重复的数据会覆盖前面的数据
System.out.println(map2);
}
}
Map常用方法
方法名 | 说明 |
---|---|
public V put(K key, V value) | 往Map集合添加元素 |
public int size() | 获取集合的大小 |
public void clear() | 清空集合 |
public boolean isEmpty() | 判断集合是否为空,为空则返回true,否则返回false |
public V get(Object key) | 根据键获取对应值 |
public V remove(Object key) | 根据键删除整个元素(删除键会返回该键对应的值) |
public boolean containsValue(Object value) | 判断是否包含某个值 |
public boolean containsKey(Object key) | 判断是否包含某个键 |
public Set keySet() | 获取Map集合的全部键 |
public Collection values() | 获取Map集合的全部值 |
public void putAll(Map<? extends K, ? extends V> m) | 将指定的其他Map集合中的所有元素复制到调用该方法的Map集合中 |
案例演示
public class MapTest2 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("贾宝玉", 20);
map.put("林黛玉", 19);
map.put("薛宝钗", 19);
map.put("王熙凤", 31);
map.put("贾政", 39);
System.out.println(map);
//public int size():获取集合的大小
System.out.println(map.size());
//public void clear():清空集合
// map.clear();
//public boolean isEmpty():判断集合是否为空,为空则返回true,否则返回false
System.out.println(map.isEmpty());
//public V get(Object key):根据键获取对应值
System.out.println(map.get("林黛玉"));
//public V remove(Object key):根据键删除整个元素(删除键会返回该键对应的值)
System.out.println(map.remove("贾政"));
//public boolean containsValue(Object value):判断是否包含某个值
//public boolean containsKey(Object key):判断是否包含某个键
map.containsValue(20);
map.containsKey("薛宝钗");
//public Set<K> keySet():获取Map集合的全部键
Set<String> names = map.keySet();
System.out.println(names);
//public Collection<V> values():获取Map集合的全部值
Collection<Integer> age = map.values();
System.out.println(age);
//把其他Map集合的数据导入(倒入)到自己集合中来
Map<String, Integer> map1 = new HashMap<>();
map1.put("张朝阳", 45);
map1.put("马化腾", 42);
map1.put("许家印", 63);
map.putAll(map1);
//{薛宝钗=19, 张朝阳=45, 许家印=63, 贾宝玉=20, 王熙凤=31, 马化腾=42, 林黛玉=19}
System.out.println(map);
//{张朝阳=45, 许家印=63, 马化腾=42}
System.out.println(map1);
}
}
Map集合的遍历方式
键找值:先获取Map集合全部的键,再通过遍历键来找值
需用到Map的KeySet(获取键)和get(获取值)方法
案例演示
public class MapTest3 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("林逸轩", 175);
map.put("季悠然", 168);
map.put("赵凌枫", 182);
map.put("钱悦心", 163);
map.put("李翰宇", 185);
//获取map的全部键
Set<String> keys = map.keySet();
System.out.println(keys);
//根据键获取对应的值
for (String key : keys) {
int height = map.get(key);
System.out.println(key + ":" + height);
}
}
}
键值对:把“键值对”看作一个整体进行遍历(难度较大)
Map提供的方法 | 说明 |
---|---|
Set<Map.Entry<K, V>> entrySet() | 获取所有“键值对”的集合 |
Map.Entry提供的方法 | 说明 |
---|---|
K getKey() | 获取键 |
V getValue() | 获取值 |
案例演示
public class MapTest4 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("林逸轩", 175);
map.put("季悠然", 168);
map.put("赵凌枫", 182);
map.put("钱悦心", 163);
map.put("李翰宇", 185);
//调用Map集合提供的entrySet方法,把Map集合转换成键值对类型的Set集合
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (Map.Entry<String, Integer> entry : entries) {
String name = entry.getKey();
int height = entry.getValue();
System.out.println(name + ":" + height);
}
}
}
Lambda:JDK8之后的新技术
需要用到Map的如下方法:
方法名 | 说明 |
---|---|
default void forEach(BiConsumer<? super K, super V> action) | 结合Lambda遍历Map集合 |
案例演示
public class MapTest5 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("林逸轩", 175);
map.put("季悠然", 168);
map.put("赵凌枫", 182);
map.put("钱悦心", 163);
map.put("李翰宇", 185);
map.forEach((K, V) -> System.out.println(K + ":" + V));
}
}