• java8中List根据某一字段去重


    实体类:

    package test;
    
    public class User {
        private String userid;
        private String username;
        private String age;
        private String address;
        
        public User(String userid, String username, String age, String address) {
            super();
            this.userid = userid;
            this.username = username;
            this.age = age;
            this.address = address;
        }
        
        public String getUserid() {
            return userid;
        }
        public void setUserid(String userid) {
            this.userid = userid;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getAge() {
            return age;
        }
        public void setAge(String age) {
            this.age = age;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        @Override
        public String toString() {
            return "User [userid=" + userid + ", username=" + username + ", age=" + age + ", address=" + address + "]";
        }
    }

    测试:

    package test;
    
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.List;
    import java.util.TreeSet;
    import java.util.stream.Collectors;
    
    public class Test1 {
    
        public static void main(String[] args) {
            List<User> userList = new ArrayList<>();
            
            userList.add(new User("1","李大锤","23","南京"));
            userList.add(new User("2","张无忌","18","西安"));
            userList.add(new User("3","刘德华","26","苏州"));
            userList.add(new User("4","郭靖","33","上海"));
            
            userList.add(new User("1","李大锤","23","南京"));    //id相同,其他数据也相同
            userList.add(new User("3","带头大哥","36","杭州"));  //id相同,其他数据不同
            
            System.out.println(userList);
            //根据userid去重
            userList = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User :: getUserid))), ArrayList::new));
            System.out.println(userList);
        }
    }

    测试结果:

    [User [userid=1, username=李大锤, age=23, address=南京], User [userid=2, username=张无忌, age=18, address=西安], User [userid=3, username=刘德华, age=26, address=苏州], User [userid=4, username=郭靖, age=33, address=上海], User [userid=1, username=李大锤, age=23, address=南京], User [userid=3, username=带头大哥, age=36, address=杭州]]
    [User [userid=1, username=李大锤, age=23, address=南京], User [userid=2, username=张无忌, age=18, address=西安], User [userid=3, username=刘德华, age=26, address=苏州], User [userid=4, username=郭靖, age=33, address=上海]]

    可以看出,此操作把后面的两个id相同的都给剔除了。若两个id一样,其他数据不同的话,留下的数据是靠前的数据,剔除的是靠后的数据。

  • 相关阅读:
    C++中字符数组和字符指针问题
    C++中的常量指针和指针常量
    C++中指针形参问题
    Xcode视图调试Debug View Hierarchy
    第3章 程序的机器级表示(深入理解计算机系统)
    第2章 信息的表示和处理(深入理解计算机系统)
    第1章 计算机系统漫游(深入理解计算机系统)
    用gcc编译.cpp文件可能出现"undefined reference to `__gxx_personality_v0'"问题的解决
    第12章 并发编程(深入理解计算机系统)
    第11章 网络编程(深入理解计算机系统)
  • 原文地址:https://www.cnblogs.com/hooly/p/11612811.html
Copyright © 2020-2023  润新知