List集合去重各种方式汇总

2020-02-24 16:01:23来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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;
  }
}

 


原文链接:https://www.cnblogs.com/weigy/p/12356749.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:javaSE学习笔记(17)---锁

下一篇:注解(Annotation)