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);
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐