需求:若获取的list集合有重叠,则去除重复的集合,然后展示
思考:若是判断重复对象,可以重写对象的equales方法进行判断,list集合则用.distinct()即可实现
思路:先把所有的集合放在一起,然后使用stream的distinct()方法去重
示例:
public static List<Test1> ss() {
List<Test1> instruments = new ArrayList<>();
instruments.add(new Test1(0.09, "3"));
instruments.add(new Test1(0.28, "3"));
instruments.add(new Test1(0.03, "3"));
instruments.add(new Test1(0.3, "3"));
instruments.add(new Test1(0.09, "3"));
instruments.add(new Test1(0.1, "3"));
instruments.add(new Test1(0.3, "3"));
instruments.add(new Test1(0.4, "3"));
if (CollectionUtils.isNotEmpty(instruments)) {
List<Test1> listAll = new ArrayList<>();
List<Test1> supportListTwoSection = instruments.stream().sorted(Comparator.comparing(Test1::getNearThreeBsDelta)).collect(Collectors.toList()).subList(0,1);
List<Test1> supportListThreeSection = instruments.stream().sorted(Comparator.comparing(Test1::getNearBsDelta)).collect(Collectors.toList()).subList(0,1);
List<Test1> supportListOneSection = instruments.stream().sorted(Comparator.comparing(Test1::getNearTwoBsDelta)).collect(Collectors.toList()).subList(0,1);
supportListTwoSection.addAll(supportListThreeSection);
supportListTwoSection.addAll(supportListOneSection);
supportListTwoSection = supportListTwoSection.stream().sorted(Comparator.comparing(Test1::getId)).distinct().collect(Collectors.toList());
System.out.println(supportListTwoSection);
return supportListTwoSection;
}else{
return Collections.emptyList();
}
}