java8如何对List<Bean>进行去重和覆盖
背景:有一批数据源从kafka给过来,接收到后需要处理,然后入库,我们用一个线程消费下来,一次消费30000条,
按照对象的概念,可以用List<Person>来表示,因为某种原因,需要根据记录的主键personId先在内存做去重(覆盖)处理
在新特性之前,正常的思路会是:list转为map,key为personId,put的时候相同的personId后面的覆盖前面的
java8新特性中,对这种情形有优雅的处理方式,我们分两种:
(1)不关心覆盖逻辑,相同personId只留一条
- public static List<Person> coverDuplicate(List<Person> sourceList) {
- if (CollectionUtils.isEmpty(sourceList)) {
- return new ArrayList<>();
- }
- List<Person> distinctList = sourceList.stream().collect(
- Collectors.collectingAndThen(
- Collectors.toCollection(
- () -> new TreeSet<>(Comparator.comparing(o -> o.getPersonId()))), ArrayList::new)
- );
- return distinctList;
- }
(2)相同的personId,后面的记录要求覆盖前面的
- public static List<Person> coverDuplicate1(List<Person> sourceList) {
- if (CollectionUtils.isEmpty(sourceList)) {
- return new ArrayList<>();
- }
- List<Person> distinctList = sourceList.stream().collect(
- Collectors.toMap(Person::getPersonId, Function.identity(), (e1, e2) -> e2)
- ).values().stream().collect(Collectors.toList());
- return distinctList;
- }
测试用例:
- public class Person{
- private String personId;
- private String name;
- private Integer operateTag;
- }
- public static void main(String[] args) {
Person p1 = new Person("1","111",1);
Person p2 = new Person ("1","222",0);
Person p3 = new Person ("3","333",1);
Person p4 = new Person ("4","444",0);
Person p5 = new Person ("4","555",1);
List<Person > sourceList = new ArrayList<>();
sourceList.add(p1);- sourceList.add(p2);
sourceList.add(p3);
sourceList.add(p4);
sourceList.add(p5);- List<Person> unique = coverDuplicate(sourceList);
unique.forEach(e -> System.out.println(e.getPersonId()+","+e.getName()+","+e.getOperateTag())); }
两种方式,打印结果如预期
java8如何对List<Bean>进行去重和覆盖的更多相关文章
- Java8 根据对象某个属性值去重
list中的对象某个属性存在重复时将重复的对象去重 //根据skuAttrValueName值去重 List<SkuValue> uniqueSkuValues = skuValues.s ...
- Java8 list根据对象某个属性去重
1. 添加方法: import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import j ...
- Spring框架配置文件中有两个相同名字的bean,最后会覆盖掉一个bean
问题容易出现在多个人合作的项目中,定义bean的名字的时候发生重复. 可以配置当bean定义重复的时候抛出异常,结束程序,强制提示更改重复的bean.
- java8 常用代码
1. 使用java8 提取出 list 中 bean 的某一属性 public static void main(String[] args) { List<Student> stuLis ...
- Spring笔记 - Bean xml装配
命名空间表 aop Provides elements for declaring aspects and for automatically proxying @AspectJannotated c ...
- List去重问题与方法
面试中经常被问到的list如何去重,用来考察你对list数据结构,以及相关方法的掌握,体现你的java基础学的是否牢固.我们大家都知道,set集合的特点就是没有重复的元素.如果集合中的数据类型是基本数 ...
- Java中5种List的去重方法及它们的效率对比,你用对了吗?
01.使用两个for循环实现List去重(有序) /**使用两个for循环实现List去重(有序) * * @param list * */ public static ...
- [spring源码学习]四、IOC源码——普通bean初始化
一.代码例子 此节开始涉及到一个bean具体生成和保存的过程,仅仅涉及到最简单的bean,代码依旧是最简单的 public static void main(String[] args) { Defa ...
- 【Spring源码解读】bean标签中的属性(二)你可能还不够了解的 abstract 属性和 parent 属性
abstract 属性说明 abstract 在java的语义里是代表抽象的意思,用来说明被修饰的类是抽象类.在Spring中bean标签里的 abstract 的含义其实也差不多,表示当前bean是 ...
随机推荐
- python中输入多个数字(代码实现)
不多说,直接上代码: list1 = [] #定义一个空列表 str1 = input("请输入数值,用空格隔开:") # list2 = str1.split(" &q ...
- 迭代器,生成器,yield,yield from理解
迭代器 说到迭代器就得想说可迭代对象Iterable,实现了__iter__()方法的对象都是可迭代对象,例如很多容器,list ,set, tuples.使用iter方法可以把一个可迭代对象变成迭代 ...
- UVA 12821 Double Shortest Paths
Double Shortest PathsAlice and Bob are walking in an ancient maze with a lot of caves and one-way pa ...
- ARC103
ARC103E Tr/ee 首先没有叶子显然不科学,\(s_n\)是1也不怎么科学,\(s_i != s_{n-i}\)同样不怎么科学 特判掉上述情况后先把root记为1,链接(root,i+1)如果 ...
- android service 样例(电话录音和获取系统当前时间)
关于android service 的具体解释请參考: android四大组件--android service具体解释.以下将用两个实例具体呈现Android Service的两种实现. 一个是st ...
- List、Map、Set三个接口存取元素时,各有什么特点
List接口以特定索引来存取元素,可以有重复元素 Set接口不可以存放重复元素(使用equals方法区分是否重复) Map接口保存的是键值对(key-value-pair)映射,映射关系可以是一对一或 ...
- ICPC2008哈尔滨-E-Gauss Elimination
题目描述 Li Zhixiang have already been in “Friendship” ocean-going freighter for three months. The excit ...
- rabbitMQ 问题
1.有时候在学习或者测试的时候,发现我在一个EXCHANGE 上面绑定了多个通道,这些通道的ROUTING_KEY 各不相同.但是从发送端 发到EXCHANGE 时,却在别的通道上面也收到了该消息, ...
- 解决Zookeeper报错:conf is not executed because it is not in the whitelist的解决办法
1.echo wchp | nc localhost 2181 ,通过路径列出服务器 watch 的详细信息,且它会输出一个与 session 相关的路径.但是出现下面的错误. [root@xg61 ...
- windows线程函数必须为全局函数或者静态函数(转)
调用CreateThread(...)创建线程时要指定所创建线程的入口函数,此入口函数只能是全局函数或者类的静态成员函数. 全局函数很容易理解,但如果是类的成员函数则必须是静态成员函数,为何, 因为类 ...