本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于Map集合体系的基本使用以及常用API的相关内容,下面一起来看一下,希望对大家有帮助。
Map集合概述和使用
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集合中使用最多的Map集合是HashMap。
重点掌握HashMap , LinkedHashMap , TreeMap。其他的后续理解。
Map集合体系特点:
Map集合的特点都是由键决定的。
Map集合的键是无序,不重复的,无索引的,值不做要求(可以重复)。
Map集合后面重复的键对应的值会覆盖前面重复键的值。
Map集合的键值对都可以为null。
Map集合实现类特点:
HashMap:元素按照键是无序,不重复,无索引,值不做要求。(与Map体系一致)
public static void main(String[] args) { Map<String, Integer> maps = new HashMap<>(); maps.put("桌子", 2); maps.put("凳子", 10); maps.put("桌子", 10); maps.put(null, null); System.out.println(maps);LinkedHashMap:元素按照键是有序,不重复,无索引,值不做要求。
public static void main(String[] args) { // Map<String, Integer> maps = new HashMap<>(); Map<String, Integer> maps = new LinkedHashMap<>(); maps.put("桌子", 2); maps.put("凳子", 10); maps.put("桌子", 10); maps.put(null, null); System.out.println(maps);TreeMap:元素是按照键排序,不重复,无索引的,值不做要求。
public static void main(String[] args) { // Map<String, Integer> maps = new HashMap<>(); Map<String, Integer> 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);
Map集合常用的API
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<String, Integer> maps = new HashMap<>();
maps.put("华为", 10);
maps.put("小米", 5);
maps.put("iPhone", 6);
maps.put("生活用品", 15);
System.out.println(maps);
remove方法, 根据键删除元素
public static void main(String[] args) {
Map<String, Integer> maps = new HashMap<>();
maps.put("华为", 10);
maps.put("小米", 5);
maps.put("iPhone", 6);
maps.put("生活用品", 15);
maps.remove("小米");
System.out.println(maps);
clear方法, 清空集合元素
public static void main(String[] args) {
Map<String, Integer> 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<String, Integer> maps = new HashMap<>();
maps.put("华为", 10);
maps.put("小米", 5);
maps.put("iPhone", 6);
maps.put("生活用品", 15);
System.out.println(maps.containsKey("华为"));
System.out.println(maps.containsKey("魅族"));
containsValue方法, 判断是否包含指定值
public static void main(String[] args) {
Map<String, Integer> maps = new HashMap<>();
maps.put("华为", 10);
maps.put("小米", 5);
maps.put("iPhone", 6);
maps.put("生活用品", 15);
System.out.println(maps.containsValue(6));
System.out.println(maps.containsValue(99));
isEmpty, 判断集合是否为空
public static void main(String[] args) {
Map<String, Integer> maps = new HashMap<>();
maps.put("华为", 10);
maps.put("小米", 5);
maps.put("iPhone", 6);
maps.put("生活用品", 15);
System.out.println(maps.isEmpty());
size方法, 集合元素的个数
public static void main(String[] args) {
Map<String, Integer> maps = new HashMap<>();
maps.put("华为", 10);
maps.put("小米", 5);
maps.put("iPhone", 6);
maps.put("生活用品", 15);
System.out.println(maps.size());
扩展方法: putAll合并其他集合, 合并遇到重复的key会进行合并
public static void main(String[] args) {
Map<String, Integer> map1 = new HashMap<>();
map1.put("java", 1);
map1.put("C语言", 2);
Map<String, Integer> map2 = new HashMap<>();
map2.put("python", 4);
map2.put("linux", 7);
map1.putAll(map2);
System.out.println(map1);