java8 中使用lambda表达式对List根据某一字段去重
【代码】java8 中使用lambda表达式对List根据某一字段去重。
·
List<test1> userList = new ArrayList<>();
userList.add(new test1("1","王五","2"));
userList.add(new test1("2","赵六","1"));
userList.add(new test1("3","刘三","6"));
userList.add(new test1("4","李四","3"));
userList.add(new test1("1","王五","2")); //id相同,其他数据也相同
userList.add(new test1("4","李明","5")); //id相同,其他数据不同
//根据userid去重
userList = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(test1 :: getId))), ArrayList::new));
System.out.println(userList);
更多推荐
所有评论(0)