当前位置:网站首页>每天睡前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);
});
}
}
边栏推荐
- 双非本科生进大厂,而我还在底层默默地爬树(上)
- Pdf document of distributed service architecture: principle + Design + practice, (collect and see again)
- 京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
- 微服务实战|Eureka注册中心及集群搭建
- 分布式锁的这三种实现方式,如何在效率和正确性之间选择?
- Redis installation and deployment (windows/linux)
- 概念到方法,绝了《统计学习方法》——第三章、k近邻法
- Long summary (code with comments) number structure (C language) -- Chapter 4, string (Part 1)
- 数构(C语言--代码有注释)——第二章、线性表(更新版)
- Talk about the secret of high performance of message queue -- zero copy technology
猜你喜欢
概念到方法,绝了《统计学习方法》——第三章、k近邻法
Number structure (C language) -- Chapter 4, compressed storage of matrices (Part 2)
Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
互联网API接口幂等设计
Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
ORA-12514问题解决方法
微服务实战|微服务网关Zuul入门与实战
微服务实战|熔断器Hystrix初体验
Microservice practice | Eureka registration center and cluster construction
随机推荐
西瓜书--第五章.神经网络
盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘
There is a problem with MySQL installation (the service already exists)
Don't look for it. All the necessary plug-ins for Chrome browser are here
Mathematics in machine learning -- point estimation (I): basic knowledge
Matplotlib swordsman - a stylist who can draw without tools and code
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
【Go实战基础】gin 如何绑定与使用 url 参数
Chrome browser plug-in fatkun installation and introduction
定时线程池实现请求合并
Micro service practice | introduction and practice of zuul, a micro service gateway
VIM operation command Encyclopedia
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
Hystrix implements request consolidation
自定義Redis連接池
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
What is the function of laravel facade
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
AMQ 4043 solution for errors when using IBM MQ remote connection
Long summary (code with comments) number structure (C language) -- Chapter 4, string (Part 1)