[英]How do I compare two lists of objects and update values of specified properties with defferent values?
I have two lists of objects like this,我有两个这样的对象列表,
List<UserDTO> userList1;
List<UserDTO> userList2;
UserDTO用户DTO
public class UserDTO {
private userId;
private name;
private phone;
private address;
}
Both lists have a list of UserDTO
.两个列表都有一个
UserDTO
列表。 I am trying to iterate through list 1 and see if there's any object with same userId
from list 1 is present in list 2. If it is present then I want to check whether address
property of both objects from list 1 and list 2 matches.我正在尝试遍历列表 1,并查看列表 2 中是否存在与列表 1 中具有相同
userId
的任何对象。如果存在,那么我想检查列表 1 和列表 2 中两个对象的address
属性是否匹配。 If it does not match then I want update address of list 1 with list 2.如果它不匹配,那么我想用列表 2 更新列表 1 的地址。
try this尝试这个
list2.stream().filter(list2obj->list2obj.getUserId()==list1obj.getUserId())
.forEach(objectWithMatchingId->{
if(list1obj.getAddress()!=objectWithMatchingId.getAddress())
list1obj.setAddress(objectWithMatchingId.getAddress());
});
});
userList2
.forEach(userObj2 -> userList1
.stream()
.filter(userObj1 -> userObj1.getUserId().equals(userObj2.getUserId())
&& !userObj1.getAddress().equals(userObj2.getAddress()))
.forEach(userObj1 -> userObj1.setAddress(userObj2.getAddress()))
);
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.