DEV_ANI
DEV_ANI

Reputation: 31

Check two different objects are equal or not

I have two arraylist. ArrayList of employee class and user class. employee class has name,age,address as fields. User class has name,age,address as fields. below are two lists

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,""));

want to check if user has same address as employee. if user don't have address then copy it from employee.

Upvotes: 0

Views: 290

Answers (3)

Agust&#237;n Samper
Agust&#237;n Samper

Reputation: 31

Maybe this can help you. If you have any questions leave me a comment

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

Upvotes: 1

Joby Wilson Mathews
Joby Wilson Mathews

Reputation: 11116

Solution using Hashmap with one iteration.

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


    }

}

Upvotes: 0

juwil
juwil

Reputation: 466

I guess you want sth. like this - get the employee with matching name, if the user with this name has no address. set the user's address to the employee's address. This to be useful implies that the name ist the unique key of both the eployee and the user. If you need the employee afterwards, you could return it from the 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()));

}

Upvotes: 0

Related Questions