如何在java 8中按列表对象的多个参数分组并删除重复的行?

这是我的代码:


public class User{

        private String id;

        private String userName;

        private Long birthDate;

        private String address;

        private boolean enabled;

        //Constructors

        // Getters && Setters

        ...

}


public class ServiceUser{

    List<User> users=new ArrayList<>();


    public ServiceUser(){

        users.add(new User("OPS","MESSI",15454222454L,"ADRESSE 1",true))

        users.add(new User("TA1","RICHARD",1245485456787L,"ADRESSE 1",true));

        users.add(new User("XA5","LANG",659854575424L,"ADRESSE 2",true));

        users.add(new User("DS7","RICHARD",1245485456787L,"ADRESSE 1",false));

        users.add(new User("OPD6","LONG",659854575424L,"ADRESSE 2",false));

        ...

    }


    protected List<User> getFiltredUsers(){

        // TODO here

    }

}

我想获得如下用户列表:


User("OPS","MESSI",15454222454L,"ADRESSE 1",true)

如何删除所有重复的行,使用相同的用户名,出生日期,地址?


Nb:用户列表由数据库返回,只是为了示例,我这样说。


牛魔王的故事
浏览 103回答 2
2回答

HUH函数

以下代码将删除重复项,并仅返回用户列表中的不同元素://used for grouping them by name, birthday and addressstatic Function<User, List<Object>> groupingComponent = user ->Arrays.<Object>asList(user.getName(), user.getBirthday(),user.getAddress());//grouping method used for grouping them by groupingComponent and frequency of itprivate static Map<Object, Long> grouping(final List<User> users) {&nbsp; &nbsp; return users.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(groupingComponent, Collectors.counting()));}//filtering method used for filtering lists if element is contained more than 1 within a listprivate static List<Object> filtering(final Map<Object, Long> map) {&nbsp; &nbsp; return map.keySet()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(key -> map.get(key) < 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());}简单的用法是:filtering(grouping(users));System.out.println(filtering(grouping(users)));更新 3:说实话,由于这三个参数(生日,姓名和地址),这有点棘手。我现在能想到的最简单方法是实现hashCode并等于User类中的方法,例如(如果用户具有相同的三个值,则将其标记为相同):@Overridepublic int hashCode() {&nbsp; &nbsp; return (43 + 777);}@Overridepublic boolean equals(Object obj) {&nbsp; &nbsp; // checks to see whether the passed object is null, or if it’s typed as a&nbsp;&nbsp; &nbsp; // different class. If it’s a different class then the objects are not equal.&nbsp; &nbsp; if (obj == null || getClass() != obj.getClass()) {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; // compares the objects’ fields.&nbsp; &nbsp; User user = (User) obj;&nbsp; &nbsp; return getName().contains(user.getName())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && getBirthday() == user.getBirthday()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && getAddress()== user.getAddress();}并按照以下方法删除所有重复项&nbsp; &nbsp;public static List<User> filter(List<User> users) {&nbsp; &nbsp; &nbsp; &nbsp; Set<User> set = new HashSet<>();&nbsp; &nbsp; &nbsp; &nbsp; List<User> uniqueUsers = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; List<User> doubleUsers = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; Map<Boolean, List<User>> partitions = users.stream().collect(Collectors.partitioningBy(user -> set.add(user) == true));&nbsp; &nbsp; &nbsp; &nbsp; uniqueUsers.addAll(partitions.get(true));&nbsp; &nbsp; &nbsp; &nbsp; doubleUsers.addAll(partitions.get(false));&nbsp; &nbsp; &nbsp; &nbsp; uniqueUsers.removeAll(doubleUsers);&nbsp; &nbsp; &nbsp; &nbsp; return uniqueUsers;&nbsp; &nbsp; }

慕工程0101907

而不是 :ListList<User>&nbsp;users=new&nbsp;ArrayList<>();您可以使用 a(我在此示例中使用了 ,):SetHashSetSet<User>&nbsp;users=new&nbsp;HashSet<>();Sets不允许重复,这样您甚至不需要过滤。参见:https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java