【问题标题】:Check two different objects are equal or not检查两个不同的对象是否相等
【发布时间】:2021-01-18 23:32:21
【问题描述】:

我有两个数组列表。员工类和用户类的ArrayList。 员工类具有姓名、年龄、地址作为字段。用户类具有姓名、年龄、地址作为字段。 下面是两个列表

List<Employee> empList = new ArrayList<Employee>();
    empList.add(new Employee("Andi",20,"NY"));
    empList.add(new Employee("Rob",22,"london"));
    empList.add(new Employee("mark",21,"berlin"));
    
List<User> userList = new ArrayList<User>();
    userList.add(new User("Andi",20,"NY"));
    userList.add(new User("Rob",22,"london"));
    userList.add(new User("mark",21,""));

想检查用户是否与员工有相同的地址。如果用户没有地址,则从员工那里复制。

【问题讨论】:

  • 您要检查它们之间是否至少存在一个公共数据?
  • 是 Mikheil 我想检查员工姓名和年龄是否与用户名和年龄匹配,如果用户对象中不存在匹配和地址我想将地址从员工复制到用户
  • 您应该将其包含在您的问题中,而不是将其隐藏在其下方的评论中。

标签: java collections


【解决方案1】:

也许这可以帮助你。有什么问题可以给我留言

for(User user: userList) {                        
    for(Employee employee: empList) {           
        if(user.getName().equals(employee.getName())) {
            if(user.getAddress() == null || user.getAddress().equals("")) {
                user.setAddress(employee.getAddress());
            }
        }
    }
}
userList.forEach(user -> System.out.println(user));

【讨论】:

  • 最好使用equals 方法进行此比较,例如user.getName().equals(employee.getName())。见stackoverflow.com/questions/767372/string-equals-versus
  • 正如 djmj 所说,我们如何使用 equals 和 hashcode 来比较两个不同的类对象,因为它们的数据相似。
  • @DEV_ANI 我不建议你使用 equals 和 hashcode 来比较两个不同的类对象,但是如果你想要并且你有两个像你一样的类具有相同的类字段,你可以在两个类以相同的方式并以下一种方式比较对象:employee.hashCode() == user.hashCode()
  • 这里没有理由对冗长的手动Iterator 进行处理。只需使用for(User user: userList) { for(Employee employee: empList) { … } }
【解决方案2】:

我猜你想要什么。像这样 - 如果具有此名称的用户没有地址,则获取具有匹配名称的员工。将用户的地址设置为员工的地址。这很有用意味着名称是员工和用户的唯一键。如果您之后需要该员工,您可以从 Optional 中将其退回。

User andi = userList.stream().filter(u -> u.getName().equals("Andi")).findFirst().orElseThrow();
if (andi.getAddress() == null){
    Optional<Employee> empForUser = empList.stream().filter(e -> andi.getName().equals(e.getName())).findFirst();
    empForUser.ifPresent(employee -> andi.setAddress(employee.getAddress()));

}

【讨论】:

    【解决方案3】:

    使用 Hashmap 一次迭代的解决方案。

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    class User {
        String name;
        int age;
        String address;
    
        public User(String name, int age, String address) {
            this.name = name;
            this.age = age;
            this.address = address;
        }
    
        public String getName() {
            return name;
        }
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", address='" + address + '\'' +
                    '}';
        }
    }
    class Employee{
        String name;
        int age;
        String address;
    
        public Employee(String name, int age, String address) {
            this.name = name;
            this.age = age;
            this.address = address;
        }
    
    
        public String getName() {
            return name;
        }
    
        public String getAddress() {
            return address;
        }
    
    
        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", address='" + address + '\'' +
                    '}';
        }
    }
    
    
    public class Test {
    
        public static void main(String args[]) throws Exception {
    
            List<Employee> empList = new ArrayList<Employee>();
            empList.add(new Employee("Andi",20,"NY"));
            empList.add(new Employee("Rob",22,"london"));
            empList.add(new Employee("mark",21,"berlin"));
    
            List<User> userList = new ArrayList<>();
            userList.add(new User("Andi",20,"NY"));
            userList.add(new User("Rob",22,"london"));
            userList.add(new User("mark",21,""));
    
            Map<String, Employee> map = empList.stream()
                    .collect(Collectors.toMap(Employee::getName, employee -> employee));
            for(User user : userList){
                Employee employee = map.get(user.getName());
                if(employee==null){
                    continue;
                }
                if(user.getAddress() == null || user.getAddress().equals("")) {
                    user.setAddress(employee.getAddress());
                }
    
            }
            userList.forEach(System.out::println);
    
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-04
      • 1970-01-01
      • 2015-06-02
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多