Java中Map集合的三种遍历方式
文章目录
- Map集合的遍历方式
- Map集合的遍历方式一: 键找值
- Map集合的遍历方式二: 键值对
- Map集合的遍历方式三: Lambda
Map集合的遍历方式
Map集合的遍历方式有3种:
方式一:键找值的方式遍历:先获取Map集合全部的键,再根据遍历键找值。
方式二:键值对的方式遍历,把“键值对“看成一个整体,难度较大。
方式三:JDK 1.8开始之后的新技术:Lambda表达式。
Map集合的遍历方式一: 键找值
先通过keySet方法, 获取Map集合的全部键的Set集合。
遍历键的Set集合,然后通过键提取对应值。
键找值涉及到的API:
方法名称 | 说明 |
---|---|
keySet() | 获取所有键的集合 |
get(Object key) | 根据键获取值 |
演示代码:
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.put("java", 20);maps.put("python", 17);// 拿到全部集合的全部键Set<String> keys = maps.keySet();// 遍历键, 根据键获取值for (String key: keys) {int value = maps.get(key);System.out.println(key + "--->" +value);}
}
Map集合的遍历方式二: 键值对
先通过entrySet方法把Map集合转换成Set集合,Set集合中每个元素都是键值对实体类型了(将键和值看成一个整体)。
遍历获取到的Set集合,然后通过getKey提取键, 以及getValue提取值。
键值对设计到的API:
方法名称 | 说明 |
---|---|
Set<Map.Entry<K,V>> entrySet() | 获取所有键值对对象的集合 |
getKey() | 获得键 |
getValue() | 获取值 |
演示代码:
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.put("java", 20);maps.put("python", 17);// 把Map集合转成Set集合Set<Map.Entry<String, Integer>> newMaps = maps.entrySet();// 遍历转成的Set集合for (Map.Entry<String, Integer> newMap : newMaps) {String key = newMap.getKey(); // 获取keyInteger value = newMap.getValue(); // 获取valueSystem.out.println(key + "--->" + value);}
}
Map集合的遍历方式三: Lambda
得益于JDK 8开始的新技术Lambda表达式,提供了一种更简单、更直接的遍历集合的方式。
Map结合Lambda遍历的API:
方法名称 | 说明 |
---|---|
forEach(BiConsumer<? super K, ? super V> action) | 结合lambda遍历Map集合 |
演示代码:
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.put("java", 20);maps.put("python", 17);// 使用forEach方法遍历对象maps.forEach(new BiConsumer<String, Integer>() {@Overridepublic void accept(String key, Integer value) {System.out.println(key + "--->" + value);}});
}
结合Lambda简化代码
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.put("java", 20);maps.put("python", 17);// 使用forEach方法集合Lambda表达式遍历对象maps.forEach((key, value) -> System.out.println(key + "--->" + value));
}
Java中Map集合的三种遍历方式相关推荐
- Java中List集合的三种遍历方式(全网最详)
Map集合:链接: Map集合的五种遍历方式及Treemap方法 Set集合:链接: Java中遍历Set集合的三种方法 TreeSet集合:链接: Java深入了解TreeSet,和迭代器遍历方法 ...
- java map遍历_Java中Map集合的两种遍历方式
Java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下Java中Map集合的两种遍历方式! 关于遍历Map集合的几 ...
- java中两种遍历集合的方式_Java中Map集合的两种遍历方式
Java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下Java中Map集合的两种遍历方式! 关于遍历Map集合的几 ...
- java map集合遍历方法,Java的Map集合的三种遍历方法
集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~ 1. package com.myTest.MapText; import java.util.Collection; i ...
- java中List集合的几种遍历方式
本文中我们给出了List集合中的四种遍历方式,分别是for循环,迭代器循环,代码及相应的注释如下: package d1_collection;import java.util.*;public cl ...
- Java中list对象的三种遍历方式
1.增强for循环 for(String str : list) {//其内部实质上还是调用了迭代器遍历方式,这种循环方式还有其他限制,不建议使用.System.out.println(str); } ...
- Java数组、集合的三种遍历方式(包懂)
1 for循环 for(int i = 0;i<arr.length;i++){System.out.print(arr[i]+" "); } 2 foreach循环,这种方 ...
- Java集合篇:Map集合的几种遍历方式及性能测试
文章目录 一.写在前面 二.正式开始探究之旅 1. Map集合一共有多少种遍历方式呢? 2.那这几种遍历方式的具体用法是啥样的呢? 2.1 keySet()方式遍历-------for循环 2.2 k ...
- Map集合的几种遍历方式
Map<String ,String> map=new HashMap<String,String>(); map.put("1","value1 ...
最新文章
- SQL Server 2005自带的Service Broker功能
- try-catch语句讲解
- Linux云服务ModuleNotFoundError: No module named 'MySQLdb'
- IE下table的innerHTML无效的处理方法
- MATLAB语音识别
- centos7卸载docker_使用 Docker 部署 Spring Cloud 项目详细步骤
- 思考…求知(判别用户输入的是否是整数)
- sqlitepython导入数据_Python导入excel数据到sqlite;
- mysql左表有右表没有_查询左表存在而右表不存在的记录
- 2022年起重机司机(限桥式起重机)考试题库模拟考试平台操作
- 割裂的前端工程师--- 2017年前端生态窥探 1
- 台湾通泰TTP223-BA6和TTP223N-BA6有什么区别?
- GIT 中如何打标签(git tag)
- Android 内存卡 / Micro SD 卡 / TF 卡 / 存储卡 剩余容量 / 剩余内存 / 可用空间、总容量的 2 种获取方式
- python中key=lambda_排序后的语法(key=lambda:…)
- Linux压缩|解压_CodingPark编程公园
- Android版本对照
- Win10的Wifi图标变成了小地球(WLAN被禁用)
- connect() failed (10061: No connection could be made because the target machine actively refused it
- Problem B – Buggy ICPC——思维,找规律