(转)java8实现对象列表去重
java8实现列表去重,java8的stream和lambda的使用实例
通过普通的方式也可以达到去重的效果,但是借助java8新特性可以很方便的实现列表去重,测试demo如下
实体类:
public class Person {
private int id;
private String name;
private int age;
public Person() {
}
public Person(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
测试:
List<Person> persons = new ArrayList();
List<Integer> ids = new ArrayList<>();//用来临时存储person的id
persons.add(new Person(, "name1", ));
persons.add(new Person(, "name2", ));
persons.add(new Person(, "name5", ));
persons.add(new Person(, "name3", ));
persons.add(new Person(, "name1", )); List<Person> personList = persons.stream().filter(// 过滤去重
v -> {
boolean flag = !ids.contains(v.getId());
ids.add(v.getId());
return flag;
}
).collect(Collectors.toList());
System.out.println(personList);
还可以实现条件过滤和列表排序:
//获取person集合中的所有大于18周岁,并排序
List<Person> personList= new ArrayList();
personList.add(new Person(, "name1", ));
personList.add(new Person(, "name2", ));
personList.add(new Person(, "name5", ));
personList.add(new Person(, "name3", ));
personList.add(new Person(, "name4", ));
personList.stream()
.filter(p -> p.getAge() >= )//获取所有18岁以上的用户
//.sorted(Comparator.comparing(Person::getAge))//依年纪升序排序
.sorted(Comparator.comparing(Person::getAge).reversed())//依年纪降序排序
.collect(Collectors.toList());
System.out.println(personList);
转自:https://www.jianshu.com/p/63d2db850a8d
(转)java8实现对象列表去重的更多相关文章
- java中List对象列表去重或取出以及排序
面试碰到几次list的去重和排序.下面介绍一种做法: 1. list去重 1.1 实体类Student List<Student>容量10k以上,要求去重复.这里Student的重复标准是 ...
- Java8从对象列表中取出某个属性的列表
List<属性值类型> 属性List = 对象List.stream().map(对象::get方法()).collect(Collectors.toList()); 例如: List&l ...
- java8 List对象集合去重
//测试数据 WaterMeter w0 = new WaterMeter(); WaterMeter w1 = new WaterMeter(); WaterMeter w2 = new Water ...
- 【Java 8】Stream.distinct() 列表去重示例
在这篇文章里,我们将提供Java8 Stream distinct()示例. distinct()返回由该流的不同元素组成的流.distinct()是Stream接口的方法. distinct()使用 ...
- day 06 列表去重, 数据类型的补充,编码,深浅copy
因为重要,所以放前面 列表去重 l1 = [1, 2, 3, 4, 5] l2 = [3, 4, 5, 6, 7] set = list(set(l1 + l2)) # set自动去重,然后变成lis ...
- spring jdbc 查询结果返回对象、对象列表
首先,需要了解spring jdbc查询时,有三种回调方式来处理查询的结果集.可以参考 使用spring的JdbcTemplate进行查询的三种回调方式的比较,写得还不错. 1.返回对象(queryF ...
- 对象列表转换为DataTable或DataTable转换为对象列表.
/**********************************************************************************/ // 说明: 数据转换工具. ...
- Python内建的对象列表
Python内建的对象列表 刚写Python肯定会遇到这样的情况,想写些什么,但又不知从何写起... 在我看来问题在于我们不知道有什么东东可以拿来玩,这里列出Python的内建对象,稍微归类了一下,多 ...
- Firemonkey绑定对象列表
在实现Firemonkey绑定对象列表的过程中,我遇到的一些现有教程当中没有提到的细节,分享一下. 1.追加对象 用Navigator插入记录,位置总是在当前记录之前插入,没有在最后追加一个对象的方法 ...
随机推荐
- Matrix Factorization in RecSys
矩阵分解在推荐系统中的应用. 参考链接:知乎. 传统SVD,Funk-SVD,Bias-SVD,SVD++. SVD奇异值分解及其意义. 漫谈奇异值分解.
- java8学习之Function与BiFunction函数式接口详解
Function接口: 上次中已经使用了Function的apply()方法,但是在这个接口中还存在三个具体实现的方法,如下: 下面来仔细的将剩下的方法学习一下: compose(): 首先来读一下该 ...
- NoSQL数据库技术实战-第1章 NoSQL的数据一致性 传统关系型数据库的ACID
在看着章节的时候,我简单的回顾了一下关系型数据库的事务处理的ACID原则,其中原子性和持久性比较好理解.由于以前没有深入去研究.关于一致性和隔离性上我产生了疑问,在整理后分析如下: 一致性:书中所 ...
- BZOJ 2243: [SDOI2011]染色 (树剖+线段树)
树链剖分后两个区间合并的时候就判一下相交颜色是否相同来算颜色段数就行了. CODE #include <vector> #include <queue> #include &l ...
- [Algorithm] Finding Prime numbers - Sieve of Eratosthenes
Given a number N, the output should be the all the prime numbers which is less than N. The solution ...
- [Python]Python3调用java代码
环境:Ubuntu16.04 桌面版 Ubuntu安装java的详细教程:https://www.cnblogs.com/ttkl/p/11933884.html 安装JPype1 pip3 inst ...
- JavaMail应用--通过javamail API实现在代码中发送邮件功能
JavaMail应用 在日常开发中,可能会引用到发邮件功能,例如在持续集成中,自动化测试运行完毕,自动将测试结果以报表的形式发送邮件给相关人.那么在Java中如何实现发邮件呢? 在java EE ...
- vue模板语法上集
模板语法上集 1.1 插值 1.1.1 文本 {{msg}} 1.1.2 html 使用v-html指令用于输出html代码 1.1.3 属性 HTML属性中的值应使用v-bind指令 1.1.4 表 ...
- LCA离线Tarjan,树上倍增入门题
离线Tarjian,来个JVxie大佬博客最近公共祖先LCA(Tarjan算法)的思考和算法实现,还有zhouzhendong大佬的LCA算法解析-Tarjan&倍增&RMQ(其实你们 ...
- webpack4 单入口文件配置 多入口文件配置 以及常用的配置
单入口文件配置 webpack.config.js const path = require('path'); const HtmlWebpackPlugin = require('html-webp ...