当前位置:网站首页>每天睡前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);
});
}
}
边栏推荐
- 破茧|一文说透什么是真正的云原生
- Microservice practice | declarative service invocation openfeign practice
- 京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
- 机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
- Chrome用户脚本管理器-Tampermonkey 油猴
- Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
- Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
- During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`
- [staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
- 一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
猜你喜欢
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
There is a problem with MySQL installation (the service already exists)
Statistical learning methods - Chapter 5, decision tree model and learning (Part 1)
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
[go practical basis] gin efficient artifact, how to bind parameters to structures
Don't look for it. All the necessary plug-ins for Chrome browser are here
Matplotlib剑客行——初相识Matplotlib
MySQL multi column in operation
微服务实战|负载均衡组件及源码分析
我服了,MySQL表500W行,居然有人不做分区?
随机推荐
告别996,IDEA中必装插件有哪些?
洞见云原生|微服务及微服务架构浅析
AMQ 4043 solution for errors when using IBM MQ remote connection
Solutions to Chinese garbled code in CMD window
MySQL事务
What is the function of laravel facade
MySql报错:unblock with mysqladmin flush-hosts
Matplotlib swordsman line - first acquaintance with Matplotlib
Talk about the secret of high performance of message queue -- zero copy technology
自定義Redis連接池
Timed thread pool implements request merging
MySQL multi column in operation
Statistical learning methods - Chapter 5, decision tree model and learning (Part 1)
Say goodbye to 996. What are the necessary plug-ins in idea?
Mathematics in machine learning -- point estimation (I): basic knowledge
Enterprise level SaaS CRM implementation
View the port of the application published by was
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
hystrix 实现请求合并
Typeerror: X () got multiple values for argument 'y‘