首页 > 编程语言 >【Java基础】Map集合概述特点、基本功能、获取功能及Map集合的两种遍历方式

【Java基础】Map集合概述特点、基本功能、获取功能及Map集合的两种遍历方式

时间:2022-10-08 21:04:48浏览次数:48  
标签:Map Java map put 键值 key 集合


目录

​一、Map集合概述和特点​

​二、Map集合的基本功能​

​三、Map集合的获取功能​

​四、Map集合的两种遍历方式​

​方式一:​

​方式二:​


一、Map集合概述和特点

Map集合概述:

interface Map<K,V> K:键的类型;V:值的类型

Map集合的特点:

◆ 键值对映射关系

◆ 一个键对应一个值

◆ 键不能重复,值可以重复

◆ 元素存储无序

Map集合的基本使用:

public class MapDemo01 { 
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String,String>();

//V put(K key, V value) 将指定的值与该映射中的指定键相关联
map.put("itheima001","小林");
map.put("itheima002","小张");
map.put("itheima003","小王");
map.put("itheima003","小刘");

//输出集合对象
System.out.println(map);
}
}

二、Map集合的基本功能

方法介绍:

方法名

说明

V put(K key,V value)

添加元素

V remove(Object key)

根据键删除键值对元素

void clear()

移除所有的键值对元素

boolean containKey(Object key)

判断集合是否包含指定的键

boolean containValue(Oject value)

判断集合是否包含指定的值

boolean isEmpty()

判断集合是否为空

int size()

集合的长度,也就是集合中键值对的个数

示例代码:

public class MapDemo02 { 
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String,String>();

//V put(K key,V value):添加元素
map.put("张无忌","赵敏");
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");

//V remove(Object key):根据键删除键值对元素
System.out.println(map.remove("郭靖"));
System.out.println(map.remove("郭襄"));

//void clear():移除所有的键值对元素
map.clear();

//boolean containsKey(Object key):判断集合是否包含指定的键
System.out.println(map.containsKey("郭靖"));
System.out.println(map.containsKey("郭襄"));

//boolean isEmpty():判断集合是否为空
System.out.println(map.isEmpty());

//int size():集合的长度,也就是集合中键值对的个数
System.out.println(map.size());

//输出集合对象
System.out.println(map);
}
}

三、Map集合的获取功能

方法介绍:

方法名

说明

V get(Object key)

根据键获取值

Set keySet()

获取所有键的集合

Collection values()

获取所有值的集合

Set<Map.Entry<K,V>> entrySet()

获取所有键值对对象的集合

示例代码:

public class MapDemo03 { 
public static void main(String[] args) {
//创建集合对象
Map<String, String> map = new HashMap<String, String>();

//添加元素
map.put("张无忌", "赵敏");
map.put("郭靖", "黄蓉");
map.put("杨过", "小龙女");

//V get(Object key):根据键获取值
System.out.println(map.get("张无忌"));
System.out.println(map.get("张三丰"));

//Set<K> keySet():获取所有键的集合
Set<String> keySet = map.keySet();
for(String key : keySet) {
System.out.println(key);
}

//Collection<V> values():获取所有值的集合
Collection<String> values = map.values();
for(String value : values) {
System.out.println(value);
}
}
}

四、Map集合的两种遍历方式

方式一:

遍历思路

map集合存储的元素是成对出现的,map集合键值对为一个集合

把所有的键集中起来

遍历键的集合,获取每一个键

根据键去找到对应的值

步骤分析:

● 获取所有的键的集合,用keySet()方法实现

● 遍历键的集合,获取到每一个键,用增强for实现

● 根据键去找值,用get(Object key)方法实现

代码实现:

public class MapDemo01 { 
public static void main(String[] args) {
//创建集合对象
Map<String, String> map = new HashMap<String, String>();

//添加元素
map.put("张无忌", "赵敏");
map.put("郭靖", "黄蓉");
map.put("杨过", "小龙女");

//获取所有键的集合。用keySet()方法实现
Set<String> keySet = map.keySet();

//遍历键的集合,获取到每一个键。用增强for实现
for (String key : keySet) {
//根据键去找值。用get(Object key)方法实现
String value = map.get(key);
System.out.println(key + "," + value);
}
}
}

方式二:

遍历思路

存储的元素是成对出现的,所有可以把map看成一个键值对的集合

获取所有的键值对集合

遍历键值对的集合,得到每一个键值对

根据键值对获取键和值

步骤分析:

获取所有键值对对象的集合

Set<Map.Entry<K,V>> entrySet():获取所有键值对对象的集合

遍历键值对对象的集合,得到每一个键值对对象

■ 用增强for实现,得到每一个Map.Entry

根据键值对对象获取键和值

■ 用getKey()得到键

■ 用getValue()得到值

代码实现:

public class MapDemo02 { 
public static void main(String[] args) {
//创建集合对象
Map<String, String> map = new HashMap<String, String>();

//添加元素
map.put("张无忌", "赵敏");
map.put("郭靖", "黄蓉");
map.put("杨过", "小龙女");

//获取所有键值对对象的集合
Set<Map.Entry<String, String>> entrySet = map.entrySet();

//遍历键值对对象的集合,得到每一个键值对对象
for (Map.Entry<String, String> me : entrySet) {
//根据键值对对象获取键和值
String key = me.getKey();
String value = me.getValue();
System.out.println(key + "," + value);
}
}
}

标签:Map,Java,map,put,键值,key,集合
From: https://blog.51cto.com/u_15815415/5738706

相关文章