List集合去重各种方式汇总
package com.sb.test; import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSON; /**
* @version 1.0.
* @className :MyTest
* @Date :2019/10/2
**/
public class MyTest {
public final static Logger logger = LoggerFactory.getLogger(MyTest.class); public static void main(String[] args) {
logger.info("------------List String 去重---------------------");
List<String> list = new ArrayList<>();
list.add("123");
list.add("432");
list.add("123");
list.stream().forEach(str -> System.out.println(str));
Set<String> set = new HashSet<>(list);
list.clear();
list.addAll(set);
list.stream().forEach(str -> System.out.println(str)); logger.info("*****************List<TestModel> 去重***********************************");
List<TestModel> list2 = new ArrayList<>();
TestModel testModel = new TestModel();
testModel.setAge(11);
testModel.setName("我爱你中国");
TestModel testModel2 = new TestModel();
testModel2.setAge(11);
testModel2.setName("我爱你中国");
list2.add(testModel);
list2.add(testModel2);
list2.stream().forEach(item -> {
logger.info(item.getName() + item.getAge());
}); logger.info("---------------根据对象的某个属性去重,例如:age-------------------------");
List<TestModel> listNew = new ArrayList<>();
Set<TestModel> testModelSet = new TreeSet<>((o1, o2) -> o1.getAge().compareTo(o2.getAge()));
testModelSet.addAll(list2);
listNew.addAll(testModelSet);
listNew.stream().forEach(item11 -> {
logger.info(item11.getName() + item11.getAge());
});
logger.info("----------------根据对象多个属性去重 属性:age + name--------------");
List<TestModel> listNew2 = new ArrayList<>();
List<TestModel> testModels = removeDupliByMoreModel(list2);
logger.info(JSON.toJSONString(testModels)); logger.info("----------------根据对象单个属性去重属性:name--------------");
List<TestModel> testModels1 = removeDupliByNameNew(list2);
logger.info(JSON.toJSONString(testModels1));
} /**
* List集合去重
*/
public static List<TestModel> removeDupliByMoreModel(List<TestModel> list) {
List<TestModel> listModel = list.stream().collect(Collectors
.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> {
return (o.getAge() + "-" + o.getName());
}))), ArrayList::new));
return new ArrayList<>(listModel);
} /**
* 根据对象单个属性去重属性:name
*/
public static List<TestModel> removeDupliByNameNew(List<TestModel> list) {
List<TestModel> personList = new ArrayList<>();
list.stream().filter(distinctByKey(p -> p.getName())).forEach(p -> personList.add(p));
return personList;
} /**
* 据key去重重复
*/
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
} }
package com.sb.test; /**
* @version 1.0.
* @className :TestModel
* @Description: TODD
* @Date :2019/10/15 0015
**/
public class TestModel {
private Integer age;
private String name; public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
List集合去重各种方式汇总的更多相关文章
- List集合去重方式及效率对比
List集合相信大家在开发过程中几乎都会用到.有时候难免会遇到集合里的数据是重复的,需要进行去除.然而,去重方式有好几种方式,你用的是哪种方式呢?去重方式效率是否是最高效.最优的呢?今天就给大家讲解一 ...
- List集合去重的一种方法
前一段时间们需要对一个List<Model>集合去重,情况是该集合中会出现多个Name属性值相同的,但是其他属性值不同的数据. 在这种情况下,需求要只保留其中一个就好. 我觉得遍历和Has ...
- List集合去重的一种方法 z
需要对一个List<Model>集合去重,情况是该集合中会出现多个Name属性值相同的,但是其他属性值不同的数据. 在这种情况下,需求要只保留其中一个就好. 我觉得遍历和HashSet都不 ...
- List集合去重
本篇包含了两种去重,一种是List集合去重,一种是两个List集合去重合并 List集合去重,一般是两种方式,一种是遍历list集合判断后赋给另一个list集合,一种是用赋给set集合再返回给list ...
- List集合去重的一些方法(常规遍历、Set去重、java8 stream去重、重写equals和hashCode方法)
1. 常规元素去重 碰到List去重的问题,除了遍历去重,我们常常想到利用Set集合不允许重复元素的特点,通过List和Set互转,来去掉重复元素. // 遍历后判断赋给另一个list集合,保持原来顺 ...
- C# List集合去重操作注意点
今天调试代码时发现list的distinct方法在对引用类型操作时并没有去重,后来查阅资料发现list去重操作对象集合时比较的是对象的一个个引用地址, 因为集合里的对象都是一个个单独的实例,所以并不会 ...
- js实现数组去重的方式(7种)
JS数组去重的方式 例:将下面数组去除重复元素(以多种数据类型为例) const arr = [1, 2, 2, 'abc', 'abc', true, true, false, false, und ...
- Map集合的遍历方式:
迭代器来遍历 : entrySet() ; keySet(); values(); eg.HashMap<String,String> map = new HashMap<Strin ...
- Android中三种超实用的滑屏方式汇总(转载)
Android中三种超实用的滑屏方式汇总 现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习惯性的有事没事的左右滑屏,也不知道在干什么...嘿嘿),由于 ...
随机推荐
- python之set集合操作
set集合天生具有去重功能 1.创建集合,集合的value类型:string.tuple.frozenset.数字等不可变类型: s1 =set()#空集合 s2=set(") s3=set ...
- [C语言学习笔记一]基本构架和变量
基本构架 所有的C程序都有一个 main 函数.其后包含在大括号中的是 main 函数的内容. main函数是程序的入口,程序运行后,先进入 main 函数,然后一次执行 main 函数体中的语句. ...
- HanLP《自然语言处理入门》笔记--3.二元语法与中文分词
笔记转载于GitHub项目:https://github.com/NLP-LOVE/Introduction-NLP 3. 二元语法与中文分词 上一章中我们实现了块儿不准的词典分词,词典分词无法消歧. ...
- 多线程之CountDownLatch的用法及原理笔记
前言-CountDownLatch是什么? CountDownLatch是具有synchronized机制的一个工具,目的是让一个或者多个线程等待,直到其他线程的一系列操作完成. CountDownL ...
- 如何最快实现物流即使查询功能-物流轨迹查询API
上一篇文章我们介绍了一个物流服务提供商,推荐大家使用快递鸟接口,主要介绍了如何注册账号,获得密钥,找不到注册地址的,我在发一下: http://kdniao.com/reg 今天我们来聊如何利用快递鸟 ...
- djiango目录文件
一.创建项目 命令:django-admin startproject mysite mysite ├── manage.py └── mysite ├── __init__.py ├ ...
- Codeforces Round #617 (Div. 3) D. Fight with Monsters
D : Fight with Monsters 题目大意 : 有一组数,每个值对应着一个怪物的 hp 值,现在有两个人,一个自己一个对手,每个人有一个攻击值, 两个人轮流攻击怪物,如果是自己将怪物先打 ...
- JAVA 调用控件开发
最近homoloCzh有个小伙伴接到一个需求说是把一个c# 写的具备扫描.调阅等功能 winfrom 影像控件嵌入到java Swing当中,让小伙伴很苦恼啊,从年前一直研究到年后,期间用了很多种方法 ...
- MongoDB入门(介绍、安装)
一.什么是MongoDB? MongoDB is a document database with the scalability and flexibility that you want with ...
- sparc v8 stack frame calling convention
main.c ; int main() { int a, b; int sum; a = ; b = ; sum = add(a, b); ; } int add(int a, int b) { in ...