java8使用stream的collect进行list转map注意事项
1.创建Person类
package com.xkzhangsan.normal.collectors;
public class Person {
private Integer id;
private String name;
private Integer score;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", score=" + score + "]";
}
}
2.创建测试类ListToMap
package com.xkzhangsan.normal.collectors; import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors; public class ListToMap { public static void main(String[] args) {
//创建list
List<Person> personList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Person p = new Person();
p.setId(i);
p.setName("p"+i);
p.setScore(i*10);
personList.add(p);
} //添加和id=8相同对象score值不同
Person p = new Person();
p.setId(8);
p.setName("p"+8);
p.setScore(88);
personList.add(p); System.out.println("list:========================");
personList.stream().forEach(System.out::println); //转换为HashMap
Map<Integer, Person> map = personList.stream().collect(Collectors.toMap(Person::getId, d->d, (oldValue, newValue)->newValue));
System.out.println("hashMap:========================");
map.entrySet().stream().forEach(System.out::println); //转换为TreeMap
Map<Integer, Person> treeMap = personList.stream().collect(Collectors.toMap(Person::getId, d->d, (oldValue, newValue)->newValue, TreeMap::new));
System.out.println("treeMap:========================");
treeMap.entrySet().stream().forEach(System.out::println);
} }
3.测试结果
list:========================
Person [id=0, name=p0, score=0]
Person [id=1, name=p1, score=10]
Person [id=2, name=p2, score=20]
Person [id=3, name=p3, score=30]
Person [id=4, name=p4, score=40]
Person [id=5, name=p5, score=50]
Person [id=6, name=p6, score=60]
Person [id=7, name=p7, score=70]
Person [id=8, name=p8, score=80]
Person [id=9, name=p9, score=90]
Person [id=8, name=p8, score=88]
hashMap:========================
0=Person [id=0, name=p0, score=0]
1=Person [id=1, name=p1, score=10]
2=Person [id=2, name=p2, score=20]
3=Person [id=3, name=p3, score=30]
4=Person [id=4, name=p4, score=40]
5=Person [id=5, name=p5, score=50]
6=Person [id=6, name=p6, score=60]
7=Person [id=7, name=p7, score=70]
8=Person [id=8, name=p8, score=88]
9=Person [id=9, name=p9, score=90]
treeMap:========================
0=Person [id=0, name=p0, score=0]
1=Person [id=1, name=p1, score=10]
2=Person [id=2, name=p2, score=20]
3=Person [id=3, name=p3, score=30]
4=Person [id=4, name=p4, score=40]
5=Person [id=5, name=p5, score=50]
6=Person [id=6, name=p6, score=60]
7=Person [id=7, name=p7, score=70]
8=Person [id=8, name=p8, score=88]
9=Person [id=9, name=p9, score=90]
4.注意事项
(1)list转map要注意重复对象,map转换方法要选择带mergeFunction参数的方法,如果key值重复,做合并处理,不然会抛异常!可以做到去重效果。
比如上面故意添加和id=8相同对象score为88,值不同。在map转换方法mergeFunction 为(oldValue, newValue)->newValue 使用新对象替换已有老对象,可以看到转换后id8的对象score变为88。
(2)list转map默认转换为HashMap,可以选择带mapSupplier参数的方法,选择要转换为的map类型。
比如上面TreeMap::new,选择转换为TreeMap。
github地址:https://github.com/xkzhangsan/java8-practice
java8使用stream的collect进行list转map注意事项的更多相关文章
- java8之stream
lambda表达式是stream的基础,初学者建议先学习lambda表达式,http://www.cnblogs.com/andywithu/p/7357069.html 1.初识stream 先来一 ...
- Java8 Lambda/Stream使用说明
一.Stream流1. 流的基本概念 1.1 什么是流?流是Java8引入的全新概念,它用来处理集合中的数据,暂且可以把它理解为一种高级集合.众所周知,集合操作非常麻烦,若要对集合进行筛选.投影,需要 ...
- Java8的Stream流(一) --- 基础用法
Java8中的Stream Stream使用一种类似用SQL语句从数据库查询数据的直观方式来提供一种对Java集合运算和表达的高阶抽象. Stream的特性及优点: 无存储. Stream不是一种数据 ...
- Java8的Stream API使用
前言 这次想介绍一下Java Stream的API使用,最近在做一个新的项目,然后终于可以从老项目的祖传代码坑里跳出来了.项目用公司自己的框架搭建完成后,我就想着把JDK版本也升级一下吧(之前的项目, ...
- JAVA8之 Stream 流(四)
如果说前面几章是函数式编程的方法论,那么 Stream 流就应该是 JAVA8 为我们提供的最佳实践. Stream 流的定义 Stream 是支持串行和并行操作的一系列元素.流操作会被组合到流管道中 ...
- Java8之Stream详解
Java8中提供了Stream对集合操作作出了极大的简化,学习了Stream之后,我们以后不用使用for循环就能对集合作出很好的操作. 一.流的初始化与转换 Java中的Stream的所有操作 ...
- java8的stream功能及常用方法
Java8中stream对集合操作做了简化,用stream操作集合能极大程度简化代码.Stream 就如同一个迭代器(Iterator),单向,不可往复,数据只能遍历一次,遍历过一次后就用尽了. 一. ...
- java8中stream的map和flatmap的理解
转自https://blog.csdn.net/wynjauu/article/details/78741093 假如我们有这样一个需求给定单词列表["Hello","W ...
- Java8的Stream方法findAny空指针异常(NullPointerException)实例对比
实战介绍 学习完Java8的Stream方法,可能你正准备大展身手,却发现遇到不少问题,本篇文章为大家带来一个findAny方法抛出java.lang.NullPointerException的场景. ...
随机推荐
- 数据库备份 DBS(Database Backup),知识点
资料 网址 什么是DBS https://help.aliyun.com/document_detail/59133.html?spm=5176.13685554.103.6.3fa463f9CDwW ...
- USACO Poker Hands
洛谷 P3078 [USACO13MAR]扑克牌型Poker Hands 题目传送门 JDOJ 2359: USACO 2013 Mar Silver 1.Poker Hands JDOJ传送门 题目 ...
- Python进阶-I 初识函数(function)
函数 在java中叫方法. 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print() ...
- yii2 Query Builder 查询打印sql语句
$query = new Query(); $query->select('gs.*, g.goods_images, sa.attr_name, sa.is_default, sa.alias ...
- BZOJ 3132: 上帝造题的七分钟 树状数组+差分
这个思路很巧妙啊 ~ code: #include <cstdio> #include <algorithm> #define N 2050 #define ll int #d ...
- 优先队列优化的 Huffman树 建立
如果用vector实现,在运行时遍历寻找最小的两个节点,时间复杂度为O(N^2) 但是我们可以用priority_queue优化,达到O(N logN)的时间复杂度 需要注意的是priority_qu ...
- LOJ6625 时间复杂度(min_25筛)
本人在LOJ的第三题(前两题太水不好意思说了QwQ),欢迎大家踩std. 题目链接:LOJ 题目大意:定义函数 $f$:($minp$ 表示最小质因子) $$f(x)=\begin{cases}0&a ...
- 【luoguP1168】中位数
题目链接 用一个大根堆和一个小根堆维护中位数即可 #include<iostream> #include<cstring> #include<cstdio> #in ...
- manacher算法笔记
模板 [模板]manacher算法 不妨先只考虑如何求长度为奇数的回文串 记\(P[i]\)表示以\(i\)为中心最多向两边扩展几个字符,满足回文 如串\(ababa\), \(P[1]=0,P[2] ...
- 利用ApplicationListener和ContextRefreshedEvent加载自己的beanPool
基本原理: 1.Spring的ApplicationListener和ContextRefreshedEvent一般都是成对出现的. 2.在IOC的容器的启动过程中,当所有的bean都已经处理完成之后 ...