jdk 8 去重

138 阅读1分钟
//需要操作的实体list

List<entity> list = new ArrayList<entity>();
 
//根据某个实体属性去重

List<entity> list2 = list.stream().filter(distinctByKey((p) -> (p.get属性名称()))).collect(Collectors.toList());
 
//根据多个实体属性去重,我只是举了两个多个的话以此类推。

List<entity> list3 = list.stream().filter(distinctByKey((p) -> (p.get属性名称1()))).filter(distinctByKey((p) -> (p.get属性名称2()))).collect(Collectors.toList());