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

时间:2019-04-09 19:35:27

标签: java 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:用户列表由数据库返回,仅用于   例如我这样说。

2 个答案:

答案 0 :(得分:2)

以下代码删除重复项并仅返回用户列表中的不同元素:

//used for grouping them by name, birthday and address
static 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 it
private static Map<Object, Long> grouping(final List<User> users) {
    return users.stream()
            .collect(Collectors.groupingBy(groupingComponent, Collectors.counting()));
}
//filtering method used for filtering lists if element is contained more than 1 within a list
private static List<Object> filtering(final Map<Object, Long> map) {
    return map.keySet()
            .stream()
            .filter(key -> map.get(key) < 2)
            .collect(Collectors.toList());
}

简单用法是:filtering(grouping(users));

System.out.println(filtering(grouping(users)));

更新3: 老实说,由于这三个参数(生日,姓名和地址),这有点棘手。我现在想到的最简单的方法是在您的User类中实现hashCode和equals方法like(如果这三个值相同,则将用户标记为相同):

@Override
public int hashCode() {
    return (43 + 777);
}
@Override
public boolean equals(Object obj) {
    // checks to see whether the passed object is null, or if it’s typed as a 
    // different class. If it’s a different class then the objects are not equal.
    if (obj == null || getClass() != obj.getClass()) {
        return false;
    }
    // compares the objects’ fields.
    User user = (User) obj;
    return getName().contains(user.getName())
            && getBirthday() == user.getBirthday()
            && getAddress()== user.getAddress();
}

以及以下删除所有重复项的方法

   public static List<User> filter(List<User> users) {
        Set<User> set = new HashSet<>();
        List<User> uniqueUsers = new ArrayList<>();
        List<User> doubleUsers = new ArrayList<>();
        Map<Boolean, List<User>> partitions = users.stream().collect(Collectors.partitioningBy(user -> set.add(user) == true));
        uniqueUsers.addAll(partitions.get(true));
        doubleUsers.addAll(partitions.get(false));
        uniqueUsers.removeAll(doubleUsers);
        return uniqueUsers;
    }

答案 1 :(得分:0)

代替List

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

您可以使用Set(在此示例中,我使用的是HashSet):

Set<User> users=new HashSet<>();

Sets不允许重复,因此您甚至不需要过滤。

另请参阅:https://docs.oracle.com/javase/7/docs/api/java/util/Set.html