当前位置:网站首页>每天睡前30分钟阅读Day5_Map中全部Key值,全部Value值获取方式
每天睡前30分钟阅读Day5_Map中全部Key值,全部Value值获取方式
2022-07-02 06:34:00 【Janson666】
如何获取map中全部key值,全部value值,以及同时获取全部key和value值?
1. 获取map中的全部键值(keySet方法),输出采用lambda表达式
Map<String, Object> attrMap = Files.readAttributes(path, "*", LinkOption.NOFOLLOW_LINKS);
//获取map中的全部键值
Set<String> keySet = attrMap.keySet();
keySet.forEach(key -> System.out.println(key));
2. 获取map中的全部 value值,(values 方法),输出采用foreach
Collection<Object> values = attrMap.values();
for (Object value: values) {
System.out.println("map中的value值为:" + value);
}
3.同时获取 key值 和 value值 (entrySet 方法)。输出采用了两种方法,iterator迭代器 或者 foreach循环,lambda表达式形式
//可以同时获取map中的 key值 和 value值
Set<Map.Entry<String, Object>> entrySet = attrMap.entrySet();
Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> next = iterator.next();
String key = next.getKey();
Object value = next.getValue();
System.out.println("key值为"+ key+ " value值为:" + value);
}
System.out.println("============采用 foreach 循环,获取键值和value值============");
entrySet.forEach(entry -> {
String key = entry.getKey();
Object value = entry.getValue();
System.out.println("key值为"+ key+ " value值为:" + value);
});
本次测试全部代码:
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
/** * @Author Janson * @Date 2022/4/20 9:17 * @Version 1.0 */
public class Day4_Files {
public static void main(String[] args) throws Exception {
Path path = Paths.get("O:\\code\\BeforeSleeping30m\\testData");
//获取文件的属性
//{@code "*"} then all attributes are read.
// LinkOption.NOFOLLOW_LINKS 不要跟随符号连接
Map<String, Object> attrMap = Files.readAttributes(path, "*", LinkOption.NOFOLLOW_LINKS);
//获取map中的全部键值
Set<String> keySet = attrMap.keySet();
keySet.forEach(key -> System.out.println(key));
//获取map中的全部 value值
Collection<Object> values = attrMap.values();
for (Object value: values) {
System.out.println("map中的value值为:" + value);
}
//可以同时获取map中的 key值 和 value值
Set<Map.Entry<String, Object>> entrySet = attrMap.entrySet();
Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> next = iterator.next();
String key = next.getKey();
Object value = next.getValue();
System.out.println("key值为"+ key+ " value值为:" + value);
}
System.out.println("============采用 foreach 循环,获取键值和value值============");
entrySet.forEach(entry -> {
String key = entry.getKey();
Object value = entry.getValue();
System.out.println("key值为"+ key+ " value值为:" + value);
});
}
}
边栏推荐
- Solutions to Chinese garbled code in CMD window
- Amq6126 problem solving ideas
- idea查看字节码配置
- Matplotlib剑客行——布局指南与多图实现(更新)
- [go practical basis] how to install and use gin
- Bold prediction: it will become the core player of 5g
- Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
- WSL installation, beautification, network agent and remote development
- 数构(C语言--代码有注释)——第二章、线性表(更新版)
- In depth analysis of how the JVM executes Hello World
猜你喜欢
![[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)](/img/7f/2cd789339237b7a881bfed7b7545a9.jpg)
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)

Troubleshooting and handling of an online problem caused by redis zadd

机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案

一篇详解带你再次重现《统计学习方法》——第二章、感知机模型

远程连接IBM MQ报错AMQ4036解决方法

Chrome video download Plug-in – video downloader for Chrome

Matplotlib剑客行——初相识Matplotlib

「Redis源码系列」关于源码阅读的学习与思考

概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法

十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
随机推荐
【Go实战基础】gin 如何设置路由
"Redis source code series" learning and thinking about source code reading
Matplotlib swordsman line - first acquaintance with Matplotlib
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
[go practical basis] gin efficient artifact, how to bind parameters to structures
Chrome用户脚本管理器-Tampermonkey 油猴
MySQL multi column in operation
Servlet全解:继承关系、生命周期、容器和请求转发与重定向等
Chrome video download Plug-in – video downloader for Chrome
机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
Flink-使用流批一体API统计单词数量
【Go实战基础】gin 如何绑定与使用 url 参数
How to use pyqt5 to make a sensitive word detection tool
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
分布式锁的这三种实现方式,如何在效率和正确性之间选择?
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
Microservice practice | fuse hytrix initial experience
MySql报错:unblock with mysqladmin flush-hosts
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数