0

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.

3
  • Do you want to check if there are at least one common data between them? Commented Jan 16, 2021 at 19:58
  • yes Mikheil I want to check whether employee name and age is matching with user name and age if its matching and address is not present in user object i want to copy address from employee to user Commented Jan 17, 2021 at 9:31
  • 1
    You should include that into your question, instead of hiding it in a comment beneath it. Commented Jan 18, 2021 at 9:06

3 Answers 3

1

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)); 
Sign up to request clarification or add additional context in comments.

4 Comments

Better use equals method for this comparison like user.getName().equals(employee.getName()). See stackoverflow.com/questions/767372/string-equals-versus
As djmj said, How we can use equals and hashcode for comparing two different class object as there data is similar.
@DEV_ANI I do not recommend you use equals and hashcode to compare two different class objects, but if you want to and you have two classes like yours which have the same class field, you could override hashcode in both classes in the same way and compare the objects in the next way: employee.hashCode() == user.hashCode()
There is no reason for verbose manual Iterator handling here. Just use for(User user: userList) { for(Employee employee: empList) { … } }
0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.