站长资讯网
最全最丰富的资讯网站

Java中Map集合体系的基本使用和常用API

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于Map集合体系的基本使用以及常用API的相关内容,下面一起来看一下,希望对大家有帮助。

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}

Java中Map集合体系的基本使用和常用API

Map集合体系的特点

Java中Map集合体系的基本使用和常用API

Map集合中使用最多的Map集合是HashMap。

重点掌握HashMap , LinkedHashMap , TreeMap。其他的后续理解。

Map集合体系特点:

Map集合的特点都是由键决定的。

Map集合的键是无序,不重复的,无索引的,值不做要求(可以重复)。

Map集合后面重复的键对应的值会覆盖前面重复键的值。

Map集合的键值对都可以为null。

Map集合实现类特点:

HashMap:元素按照键是无序,不重复,无索引,值不做要求。(与Map体系一致)

public static void main(String[] args) {     // 创建一个HashMap对象     Map<String, Integer> maps = 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对象     // 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); // 键值对可以为null      // 输出集合, 是有序的     System.out.println(maps); // {桌子=10, 凳子=10, null=null}}
登录后复制

TreeMap:元素是按照键排序,不重复,无索引的,值不做要求。

public static void main(String[] args) {     // 创建一个HashMap对象     // Map<String, Integer> maps = new HashMap<>();     // Map<String, Integer> maps = new LinkedHashMap<>();     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); // {aaa=5, bbb=10, ccc=1, ddd=3}}
登录后复制

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集合对象     Map<String, Integer> 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<String, Integer> 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<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集合对象     Map<String, Integer> maps = new HashMap<>();      // 添加元素     maps.put("华为", 10);     maps.put("小米", 5);     maps.put("iPhone", 6);     maps.put("生活用品", 15);        // 判断是否包含指定键     System.out.println(maps.containsKey("华为")); // true     System.out.println(maps.containsKey("魅族")); // false}
登录后复制

containsValue方法, 判断是否包含指定值

public static void main(String[] args) {     // 创建Map集合对象     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)); // true     System.out.println(maps.containsValue(99)); // false}
登录后复制

isEmpty, 判断集合是否为空

public static void main(String[] args) {     // 创建Map集合对象     Map<String, Integer> 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<String, Integer> 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<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); // {{python=4, java=7, C语言=2}}
登录后复制

推荐学习:《java视频教程》

赞(0)
分享到: 更多 (0)