目录
1. 了解Map集合
private static void mapTest() {
// 无序,不重复,无索引
// Map<String, Integer> map = new HashMap<>();
Map<String, Integer> map = new LinkedHashMap<>();
// 1. 增加数据
map.put("手表", 100);
map.put("手表", 200); // 后面重复的数据会覆盖前面的数据
map.put("手机", 1200);
map.put("Java", 100);
map.put(null, null);
System.out.println(map);
// 2. TreeMap
Map<Integer, String> map1 = new TreeMap<>();
map1.put(23, "Java");
map1.put(23, "python");
map1.put(19, "C++");
map1.put(20, "VSCode");
System.out.println(map1);
}
2. Map集合的常用方法
方法 | 说明 |
---|---|
public int size() | 获取集合的大小 |
public void clear() | 清空集合 |
public boolean isEmpty() | 判断集合是否为空,为空返回True,否则返回False |
public V get(Object key) | 根据键获取对应的值 |
public V remove(Object key) | 根据键删除对应的整个元素(删除键会返回键的值) |
public boolean containsKey(Object key) | 判断是否包含某个键,包含返回True,否则返回False |
public boolean containsValue(Object key) | 判断是否包含某个值,包含返回True,否则返回False |
public Set keySet() | 获取Map集合的全部键 |
public Collection values() | 获取Map集合的所有值 |
map1.pushAll(map2) | 将其他Map集合的数据导入到自己的Map集合中来 |
Set<Map.Entry<K,V>> entrySet() | 获取所有"键值对"的集合 |
private static void mapAPI() {
Map<String, String> map = new HashMap<>();
map.put("name", "张三");
map.put("age", "23");
map.put("sex", "男");
System.out.println(map);
// 1. public int size() 获取集合的大小
System.out.println(map.size());
// 2. public void clear() 清空集合
// map.clear();
// System.out.println(map);
// 3. public boolean isEmpty() 判断集合是否为空,为空返回True,否则返回False
System.out.println(map.isEmpty());
// 4. public V get(Object key) 根据键获取对应的值
System.out.println(map.get("name"));
// 5. public V remove(Object key) 根据键删除对应的整个元素(删除键会返回键的值)
map.remove("age");
System.out.println(map);
// 6. public boolean containsKey(Object key) 判断是否包含某个键,包含返回True,否则返回False
System.out.println(map.containsKey("name"));
// 7. public boolean containsValue(Object key) 判断是否包含某个值,包含返回True,否则返回False
System.out.println(map.containsValue("lisi"));
// 8. public Set<K> keySet() 获取Map集合的全部键
System.out.println(map.keySet());
// 9. public Collection<V> values() 获取Map集合的所有值
System.out.println(map.values());
// 10. 将其他Map集合的数据导入到自己的Map集合中来
Map<String, String> map1 = new HashMap<>();
map1.put("address", "北京朝阳");
map1.put("height", "180");
map1.put("weight", "160");
map.putAll(map1);
System.out.println(map);
}
3. Map集合的遍历
3.1 键找值
Map<String, String> map = new HashMap<>();
map.put("name", "张三");
map.put("age", "23");
map.put("sex", "男");
Set<String> keySet = map.keySet();
for (String s : keySet) {
System.out.println(s + ":" + map.get(s));
}
3.2 键值对
Map<String, String> map = new HashMap<>();
map.put("name", "张三");
map.put("age", "23");
map.put("sex", "男");
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ":" + value);
}
3.3 Lambda表达式
Map<String, String> map = new HashMap<>();
map.put("name", "张三");
map.put("age", "23");
map.put("sex", "男");
map.forEach((k, v) -> {
System.out.println(k + ":" + v);
});
4. HashMap
5. LinkedHashMap
6. TreeMap
7. 集合嵌套
要求在程序中记录如下信息:
江苏省=“南京”,“扬州”,“苏州”,“无锡”,“常州”
湖北省=“武汉”,“孝感”,“十X”,“宜昌”,“鄂州”
河北省=“石家庄”,“唐山”,“邢台”,“保定”,“张家口”
private static void nestTest() {
HashMap<String, ArrayList<String>> listHashMap = new HashMap<>();
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"南京","扬州","苏州","无锡","常州");
listHashMap.put("江苏省", list);
ArrayList<String> list1 = new ArrayList<>();
Collections.addAll(list1,"武汉","孝感","十X","宜昌","鄂州");
listHashMap.put("湖北省", list1);
ArrayList<String> list2 = new ArrayList<>();
Collections.addAll(list2,"石家庄","唐山","邢台","保定","张家口");
listHashMap.put("河北省", list2);
System.out.println(listHashMap);
System.out.println("=================================================");
ArrayList<String> list3 = listHashMap.get("江苏省");
for (String s : list3) {
System.out.println(s);
}
System.out.println("=================================================");
listHashMap.forEach((k, v) -> {
System.out.println(k + ":" + v);
});
}