java8根据对象属性去重,list<bean> 使用java8根据某几个属性去重复

1:需求

根据bean对象的某几个属性去重

2:distinct()的不足

distinct是根据bean的hash与equals方法去重,达不到本次需求的要求

3:数据准备

@Data

public class Dish {

private String name; //菜的名称

private Boolean vegetaian; //是否为素

private Integer calories; //卡路里

private Type type; //类型(肉 鱼 其他)

public Dish() {

}

public Dish(String name, Boolean vegetaian, Integer calories, Type type) {

this.name = name;

this.vegetaian = vegetaian;

this.calories = calories;

this.type = type;

}

public enum Type {MEAT, FISH, OTHER} //肉 鱼 其他

}

public class DishList {

//数据准备

public static List getDish1List() {

return Arrays.asList(

new Dish("pork", false, 800, Dish.Type.MEAT),

new Dish("pork", false, 800, Dish.Type.MEAT),

new Dish("beef", false, 700, Dish.Type.MEAT),

new Dish("beef", false, 701, Dish.Type.MEAT),

new Dish("chicken", false, 400, Dish.Type.MEAT),

new Dish("french fries", true, 530, Dish.Type.OTHER),

new Dish("rice", true, 350, Dish.Type.OTHER),

new Dish("season fruit", true, 120, Dish.Type.OTHER),

new Dish("pizza", true, 550, Dish.Type.OTHER),

new Dish("prawns", false, 300, Dish.Type.FISH),

new Dish("salmon", false, 450, Dish.Type.FISH)

);

}

}

4:去重要求

根据type-name去重的

/**

* 根据type-name去重的Function

*

* @return

*/

public static Function distinctByKeyFunction() {

return (Dish dish) -> dish.getType() + "-" + dish.getName();

}

5:解决方案

方案1:使用TreeSet

@Test

public void distinctByKey1() {

List dish1List = DishList.getDish1List();

List distinctDishList = dish1List.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(distinctByKeyFunction()))), ArrayList::new));

System.out.println(distinctDishList);

}

先将list转成TreeSet(TreeSet含有比较器)过滤重复数据,再将转换为 ArrayList

collectingAndThen的使用链接

方案二:封装通用方法

方法:

public static Predicate distinctByKey(Function super T, ?> function) {

Map seen = new ConcurrentHashMap<>();

return t -> seen.putIfAbsent(function.apply(t), Boolean.TRUE) == null;

}

测试:

@Test

public void distinctByKey2() {

List dish1List = DishList.getDish1List();

List distinctDishList = dish1List.stream().filter(distinctByKey(distinctByKeyFunction())).collect(Collectors.toList());

System.out.println(distinctDishList);

}

方法2辅助理解

public static Predicate distinctByKeyOriginal() {

Map seen = new ConcurrentHashMap<>();

System.out.println(seen.size());

Predicate predicate = new Predicate() {

@Override

public boolean test(Dish dish) {

String key = dish.getType() + "" + dish.getName();

Boolean containsKey = seen.containsKey(key);

if (Boolean.TRUE.equals(containsKey)) {

System.out.println(false);

return false;

} else {

seen.put(key, Boolean.TRUE);

System.out.println(true);

return true;

}

}

};

return predicate;

}

String key = dish.getType() + “” + dish.getName(); 将其摘出,使用Function自由组合,就成了 “function.apply(t)”。

t -> seen.putIfAbsent(function.apply(t), Boolean.TRUE) == null;使用匿名内部类形式替换

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值