当前位置:网站首页>每天睡前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);
});
}
}
边栏推荐
- Win10 uses docker to pull the redis image and reports an error read only file system: unknown
- Chrome浏览器标签管理插件–OneTab
- 机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
- CKEditor 4.10.1 上传图片提示“不正确的服务器响应” 问题解决
- 十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
- MySQL error: unblock with mysqladmin flush hosts
- Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别
- Knife4j 2.X版本文件上传无选择文件控件问题解决
- "Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
- 深入剖析JVM是如何执行Hello World的
猜你喜欢

定时线程池实现请求合并

Microservice practice | fuse hytrix initial experience

Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer

Actual combat of microservices | discovery and invocation of original ecosystem implementation services

十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?

Flink-使用流批一体API统计单词数量

During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`

微服务实战|声明式服务调用OpenFeign实践

Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions

Hystrix implements request consolidation
随机推荐
What is the function of laravel facade
【Go实战基础】如何安装和使用 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
[go practical basis] how to set the route in gin
机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
Safety production early warning system software - Download safety production app software
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
西瓜书--第六章.支持向量机(SVM)
Micro service practice | introduction and practice of zuul, a micro service gateway
微服务实战|Eureka注册中心及集群搭建
Microservice practice | Eureka registration center and cluster construction
Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
Methods of classfile
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
Redis installation and deployment (windows/linux)
Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别
Actual combat of microservices | discovery and invocation of original ecosystem implementation services
Cartoon rendering - average normal stroke
Matplotlib swordsman - a stylist who can draw without tools and code
Cloud computing in my eyes - PAAS (platform as a service)