[英]How to compare two different sizes List Objects Java
I have two Lists, List A and List B having different sizes.我有两个列表,列表 A 和列表 B,它们的大小不同。 List A is getting parsed from file and List B is fetching data from the database.列表 A 正在从文件中解析,列表 B 正在从数据库中获取数据。
class A{
private String id;
private String mobile;
}
class B{
private String id;
private String name;
private String address;
private String mobile;
private String pincode;
}
Now I want to Compare both the list and want to remove the respective ids from List A which are having the same mobile number as ListB .现在我想比较两个列表,并想从列表 A中删除与ListB具有相同手机号码的相应 ID。
Tried below code试过下面的代码
private List<A> compareList(List<A> listA, List<B> listB){
List<A> temp = new ArrayList<>();
for(A a : listA){
for(B b : listB){
if(a.getId().equals(b.getId()) && !a.getMobile().equals(b.getMobile())){
temp.add(a);
}
}
}
return temp;
}
Can someone please guide me?有人可以指导我吗?
Your method creates a new list, instead of removing items from the existing list.您的方法创建一个新列表,而不是从现有列表中删除项目。 Assuming you actually want to remove items, this is one way you can do it with the Java 8 streams API: remove an item from listA if it has the same mobile
as an item in listB:假设您确实想要删除项目,这是您可以使用 Java 8 流 API 执行此操作的一种方法:如果项目与列表 B 中的项目具有相同的mobile
,则从列表 A 中删除该项目:
listA.removeIf(a -> listB.stream()
.anyMatch(b -> Objects.equals(a.getMobile(), b.getMobile())));
The streams API in this case is a little difficult to read.在这种情况下,流 API 有点难以阅读。 Here's the same without using a stream:不使用 stream 也是一样的:
for (B b : listB) {
listA.removeIf(a -> Objects.equals(a.mobile, b.mobile));
}
You can use a flag for existence and add in temp if not exists then temp contains only those elements of A which do not exist in B您可以使用标志表示存在并在 temp 中添加,如果不存在则 temp 仅包含 A 中不存在于 B 中的那些元素
List<A> temp = new ArrayList<>();
for(A a : listA){
boolean isExist = false;
for(B b : listB){
if(a.getId().equals(b.getId()) && a.getMobile().equals(b.getMobile())){
isExist = true; // if exist in List of B
break;
}
}
if(!isExist){ // if not exist in B then add in list
temp.add(a);
}
}
Note: In question you say to compare only mobile number but in code you are comparing id also, remove id equal check if you don't want it注意:有问题你说只比较手机号码,但在代码中你也在比较 id,如果你不想要它,请删除 id equal 检查
I have two Lists, List A and List B having different sizes.我有两个大小不同的列表,列表 A 和列表 B。 List A is getting parsed from file and List B is fetching data from the database.列表 A 正在从文件中解析,列表 B 正在从数据库中获取数据。
class A{
private String id;
private String mobile;
}
class B{
private String id;
private String name;
private String address;
private String mobile;
private String pincode;
}
Now I want to Compare both the list and want to remove the respective ids from List A which are having the same mobile number as ListB .现在我想比较两个列表,并希望从List A中删除与ListB具有相同手机号码的各个 ID。
Tried below code试过下面的代码
private List<A> compareList(List<A> listA, List<B> listB){
List<A> temp = new ArrayList<>();
for(A a : listA){
for(B b : listB){
if(a.getId().equals(b.getId()) && !a.getMobile().equals(b.getMobile())){
temp.add(a);
}
}
}
return temp;
}
Can someone please guide me?有人可以指导我吗?
I think you should keep the mobiles separately, as they can probably be repeated.我认为您应该将手机分开存放,因为它们可能会重复使用。 And after that compare the main collection with this list of mobiles.然后将主要集合与此手机列表进行比较。
private static List<A> compareList(List<A> listA, List<B> listB) {
List<String> mobiles = listB.stream()
.map(B::getMobile)
.distinct()
.collect(Collectors.toList());
return listA.stream()
.filter(entity -> !mobiles.contains(entity.getMobile()))
.collect(Collectors.toList());
}
To create new filtered List创建新的过滤列表
Collect all element in new list if condition does not match如果条件不匹配,则收集新列表中的所有元素
List<A> filteredList =
aList.stream()
.filter(Predicate.not(a -> bList.stream().anyMatch(b -> a.getId().equals(b.getId()) && a.getMobile().equals(b.getMobile()))))
.collect(Collectors.toList());
For in place replacement对于就地更换
aList.removeIf(a -> bList.stream().anyMatch(b -> a.getId().equals(b.getId()) && a.getMobile().equals(b.getMobile())));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.