我有两个数组列表。员工类和用户类的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,""));
想检查用户是否与员工地址相同。如果用户没有地址,则从员工那里复制。
答案 0 :(得分: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));
答案 1 :(得分:0)
我猜你想要什么。像这样 - 如果具有此名称的用户没有地址,则获取具有匹配名称的员工。将用户地址设置为员工地址。这很有用意味着名称是员工和用户的唯一键。如果您之后需要该员工,您可以从 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()));
}
答案 2 :(得分:0)
一次迭代使用 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);
}
}