当前位置:网站首页>每天睡前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);
});
}
}
边栏推荐
- 自定義Redis連接池
- [go practical basis] how to set the route in gin
- Learn combinelatest through a practical example
- Microservice practice | fuse hytrix initial experience
- Don't look for it. All the necessary plug-ins for Chrome browser are here
- Matplotlib swordsman line - layout guide and multi map implementation (Updated)
- 分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
- There is a problem with MySQL installation (the service already exists)
- Chrome浏览器标签管理插件–OneTab
- 【Go实战基础】gin 如何验证请求参数
猜你喜欢

Redis installation and deployment (windows/linux)

Timed thread pool implements request merging

京东面试官问:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别

Chrome视频下载插件–Video Downloader for Chrome

Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
![[go practical basis] how can gin get the request parameters of get and post](/img/fd/66074d157d93bcf20a5d3b37da9b3e.png)
[go practical basis] how can gin get the request parameters of get and post
![[go practical basis] how to verify request parameters in gin](/img/de/50db131d6993e5d955e3416c667c4c.png)
[go practical basis] how to verify request parameters in gin
![[staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the](/img/dc/c0ea188ef353ded86759dbe9b29df3.jpg)
[staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the

Say goodbye to 996. What are the necessary plug-ins in idea?

WSL installation, beautification, network agent and remote development
随机推荐
洞见云原生|微服务及微服务架构浅析
How to choose between efficiency and correctness of these three implementation methods of distributed locks?
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
【Go实战基础】gin 高效神器,如何将参数绑定到结构体
使用IBM MQ远程连接时报错AMQ 4043解决思路
我服了,MySQL表500W行,居然有人不做分区?
VIM operation command Encyclopedia
Break the cocoon | one article explains what is the real cloud primordial
JVM指令助记符
Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
What is the function of laravel facade
Chrome user script manager tempermonkey monkey
Jingdong senior engineer has developed for ten years and compiled "core technology of 100 million traffic website architecture"
Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
Flink - use the streaming batch API to count the number of words
长篇总结(代码有注释)数构(C语言)——第四章、串(上)
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
Amq6126 problem solving ideas
"Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
Statistical learning methods - Chapter 5, decision tree model and learning (Part 1)