当前位置:网站首页>The Map of traversal methods
The Map of traversal methods
2022-08-06 06:36:00 【Students from three provinces】

public static void main(String[] args) {
Map<String, String> map = Maps.newHashMap();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
// 方法一 Iterator
System.out.println("迭代器");
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
System.out.println("key=" + entry.getKey() + " value=" + entry.getValue());
}
// 方法二 entrySet
System.out.println("entrySet");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key=" + entry.getKey() + " value=" + entry.getValue());
}
// 方法三 keySet
System.out.println("entrySet");
for (String s : map.keySet()) {
System.out.println("key=" + s + " value=" + map.get(s));
}
// 方法四 values
System.out.println("values");
for (String s : map.values()) {
System.out.println("value =" + s);
}
//方法五 forEach
System.out.println("forEach");
map.forEach((key,value) ->{
System.out.println(key+":"+value);
});
}
Map接口概述
Map是一个将键映射到值的对象.映射不能包含重复的键:每个键最多可以映射到一个值.It models mathematical function abstractions.该MapInterfaces are included for basic operations(例如put、get、remove、 containsKey、containsValue、size和empty)、批量操作(例如putAll和clear)and collection view(例如keySet、entrySet和values)的方法.
Java The platform contains three genericsMap实现: HashMap、 TreeMap和 LinkedHashMap.Their behavior and performance are the sameSetas described in the interface HashSet、TreeSet和LinkedHashSet完全类似.
Map集合Lambda操作:
// Group employees by department
Map<Department, List<Employee>> byDept = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
// Calculate the sum of wages by department
Map<Department, Integer> totalByDept = employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));
// Divide students into qualified and unqualified
Map<Boolean, List<Student>> passFailing = students.stream()
.collect(Collectors.partitioningBy(s -> s.getGrade()>= PASS_THRESHOLD));
// 按城市对 Person 对象进行分类
Map<String, List<Person>> peopleByCity
= personStream.collect(Collectors.groupingBy(Person::getCity));
// Cascading collectors
Map<String, Map<String, List<Person>>> peopleByStateAndCity
= personStream.collect(Collectors.groupingBy(Person::getState,
Collectors.groupingBy(Person::getCity)))
亦余心之所善兮,虽九死其犹未悔.
边栏推荐
- 【R语言】环境配置Anaconda + R4.1.3 + Pycharm
- 分层架构&SOA架构
- 【RGBD视觉】
- 关于Warning:Implicit declaration of function “xxx” is invalid in C99警告!
- ReadTimeoutError or THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE when the pip command installs the toolkit
- QT5.14 实现简单用户登录系统
- ECCV 2022 Oral | 满分文章!视频实例分割新SOTA:SeqFormer & IDOL
- 6.18上午CVPR直播 | 清华三维视觉研究团队:三维人体重建与渲染、高精度人脸生成
- 7月17日上午,阿里AE技术团队直播专场,分享CVPR挑战赛冠军、亚军方案!
- 如何提高UDP传输的可靠性(三大方式RUDP、RTP、UDT)
猜你喜欢
随机推荐
srs流媒体服务器推流的流程
好的架构是进化来的,不是设计来的
利用R通过顺企网根据公司名称爬取企业地址
WebRTC视频采集模块
Cross-compilation libcurl + the openssl library
【Matlab小问题】matlab启动时出现Warning: Name is nonexistent or not a directory
R语言Logist回归
新朋老友齐聚首,共话「图形学」未来 | 将门行动派特别直播企划,就在7月6日晚!
rtcp中的持续性丢包统计
二分法的基本模板
[R] language environment configuration Anaconda + R4.1.3 + Pycharm
KDD 2022 | 中科院计算所提出STABLE:一种无监督高鲁棒性图结构学习框架
WebRTC新增FFmpeg视频编解码模块
pip命令安装工具包时出现ReadTimeoutError或者THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE问题解决
ffmpeg RGB raw数据H264编码写mp4
面试准备SL
利用R语言OLS回归分析
ReadTimeoutError or THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE when the pip command installs the toolkit
7.23 ICML直播 | 浙江大学DCD实验室况琨团队:元学习去混杂、多智能体强化学习、因果推理
Pearson相关系数R代码实现









