Map集合是一种双列集合,每个元素包含两个数据。
Map集合的每个元素的格式:key=value(键值对元素)。
Map集合也被称为“键值对集合”。
Map集合整体格式:
Collection集合的格式:
[元素1,元素2,元素3..]Map集合的完整格式:
{key1=value1 , key2=value2 , key3=value3 , ...}
Map集合的使用场景之一:购物车系统
分析:
购物车提供的四个商品和购买的数量在后台需要容器存储。
每个商品对象都一一对应一个购买数量。
把商品对象看成是Map集合的建,购买数量看成Map集合的值。
例如:
{商品1=2 , 商品2=3 , 商品3 = 2 , 商品4= 3}


Map集合中使用最多的Map集合是HashMap。
重点掌握HashMap , LinkedHashMap , TreeMap。其他的后续理解。
Map集合体系特点:
Map集合的特点都是由键决定的。
Map集合的键是无序,不重复的,无索引的,值不做要求(可以重复)。
Map集合后面重复的键对应的值会覆盖前面重复键的值。
Map集合的键值对都可以为null。
Map集合实现类特点:
HashMap:元素按照键是无序,不重复,无索引,值不做要求。(与Map体系一致)
public static void main(String[] args) {// 创建一个HashMap对象Mapmaps = new HashMap<>();// 向集合添加元素maps.put("桌子", 2);maps.put("凳子", 10);maps.put("桌子", 10); // 键一样会覆盖前面的maps.put(null, null); // 键值对可以为null// 输出集合, 可以发现是无序的System.out.println(maps); // {null=null, 凳子=10, 桌子=10} } LinkedHashMap:元素按照键是有序,不重复,无索引,值不做要求。
public static void main(String[] args) {// 创建一个LinkedHashMap对象// Mapmaps = new HashMap<>();Map maps = new LinkedHashMap<>();// 向集合添加元素maps.put("桌子", 2);maps.put("凳子", 10);maps.put("桌子", 10); // 键一样会覆盖前面的maps.put(null, null); // 键值对可以为null// 输出集合, 是有序的System.out.println(maps); // {桌子=10, 凳子=10, null=null} } TreeMap:元素是按照键排序,不重复,无索引的,值不做要求。
public static void main(String[] args) {// 创建一个HashMap对象// Mapmaps = new HashMap<>();// Map maps = new LinkedHashMap<>();Map maps = new TreeMap<>();// 向集合添加元素maps.put("ddd", 2);maps.put("bbb", 10);maps.put("ddd", 3);maps.put("aaa", 5);maps.put("ccc", 1);// 输出集合, 元素按照键进行排序System.out.println(maps); // {aaa=5, bbb=10, ccc=1, ddd=3} }
Map集合:
Map是双列集合的祖宗接口,它的功能是全部双列集合都可以继承使用的。
Map API如下:
| 方法名称 | 说明 |
|---|---|
| put(K key,V value) | 添加元素 |
| remove(Object key) | 根据键, 删除键值对元素 |
| clear() | 移除所有的键值对元素 |
| containsKey(Object key) | 判断集合是否包含指定的键 |
| containsValue(Object value) | 判断集合是否包含指定的值 |
| isEmpty() | 判断集合是否为空 |
| size() | 集合的长度,也就是集合中键值对的个数 |
put方法添加元素
public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);System.out.println(maps); // {iPhone=6, 生活用品=15, 华为=10, 小米=5}
}
remove方法, 根据键删除元素
public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 删除元素maps.remove("小米");System.out.println(maps); // {iPhone=6, 生活用品=15, 华为=10}
}
clear方法, 清空集合元素
public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 清空元素maps.clear();System.out.println(maps); // {}
}
containsKey()方法, 判断是否包含指定键
public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 判断是否包含指定键System.out.println(maps.containsKey("华为")); // trueSystem.out.println(maps.containsKey("魅族")); // false
}
containsValue方法, 判断是否包含指定值
public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 判断是否包含指定值System.out.println(maps.containsValue(6)); // trueSystem.out.println(maps.containsValue(99)); // false
}
isEmpty, 判断集合是否为空
public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 判断集合是否为空System.out.println(maps.isEmpty()); // false
}
size方法, 集合元素的个数
public static void main(String[] args) {// 创建Map集合对象Map maps = new HashMap<>();// 添加元素maps.put("华为", 10);maps.put("小米", 5);maps.put("iPhone", 6);maps.put("生活用品", 15);// 返回集合元素的个数System.out.println(maps.size()); // 4
}
扩展方法: putAll合并其他集合, 合并遇到重复的key会进行合并
public static void main(String[] args) {Map map1 = new HashMap<>();map1.put("java", 1);map1.put("C语言", 2);Map map2 = new HashMap<>();map2.put("python", 4);map2.put("linux", 7);// 合并两个集合map1.putAll(map2);System.out.println(map1); // {{python=4, java=7, C语言=2}
}
上一篇:判断一份好的问卷有哪些标准?
下一篇:算法前缀和