英文:
How to compare two different sizes List Objects Java
问题
我有两个列表,列表A和列表B,它们的大小不同。列表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;
}
现在我想要**比较**这两个列表,并且想要从**列表A**中删除与**列表B**中具有相同手机号的相应id。
**尝试了以下代码**
```java
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;
}
有人可以指导我吗?
<details>
<summary>英文:</summary>
I have two Lists, List A and List B having different sizes. List A is getting parsed from file and List B is fetching data from the database.
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**.
**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?
</details>
# 答案1
**得分**: 3
你的方法会创建一个新列表,而不是从现有列表中删除项目。假设你真的想要删除项目,这是你可以使用Java 8 streams API执行的一种方式:如果listB中的某个项目具有与listA中的某个项目相同的“mobile”,则从listA中删除一个项目:
```java
listA.removeIf(a -> listB.stream()
.anyMatch(b -> Objects.equals(a.getMobile(), b.getMobile())));
在这种情况下,使用流的API可能有些难以阅读。这里是同样的操作,但不使用流:
for (B b : listB) {
listA.removeIf(a -> Objects.equals(a.mobile, b.mobile));
}
英文:
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:
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. Here's the same without using a stream:
for (B b : listB) {
listA.removeIf(a -> Objects.equals(a.mobile, b.mobile));
}
答案2
得分: 1
你可以使用一个标志来表示存在性,并且如果不存在,将元素添加到临时列表中,那么临时列表只包含那些在 A 中存在但在 B 中不存在的元素。
List<A> temp = new ArrayList<>();
for (A a : listA) {
boolean isExist = false;
for (B b : listB) {
if (a.getMobile().equals(b.getMobile())) {
isExist = true; // 如果存在于 B 列表中
break;
}
}
if (!isExist) { // 如果在 B 中不存在,则添加到列表中
temp.add(a);
}
}
注意: 在问题中,您只说要比较手机号码,但在代码中您还在比较ID,如果您不想要ID相等的检查,请将ID相等的部分删除。
英文:
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
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
答案3
得分: 0
我认为你应该将手机分开存放,因为它们很可能会重复。之后,将主要集合与这个手机列表进行比较。
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());
}
英文:
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());
}
答案4
得分: 0
**创建新的筛选列表**
如果条件不匹配,则将所有元素收集到新列表中
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());
**用于原地替换**
aList.removeIf(a -> bList.stream().anyMatch(b -> a.getId().equals(b.getId()) && a.getMobile().equals(b.getMobile())));
英文:
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())));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论