如何比较两个不同大小的 Java 列表对象

huangapple go评论78阅读模式
英文:

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 -&gt; listB.stream()
                         .anyMatch(b -&gt; 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 -&gt; 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&lt;A&gt; temp = new ArrayList&lt;&gt;();
for(A a : listA){
    boolean isExist = false;
    for(B b : listB){
        if(a.getId().equals(b.getId()) &amp;&amp; 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&lt;A&gt; compareList(List&lt;A&gt; listA, List&lt;B&gt; listB) {
    List&lt;String&gt; mobiles = listB.stream()
            .map(B::getMobile)
            .distinct()
            .collect(Collectors.toList());

    return listA.stream()
            .filter(entity -&gt; !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&lt;A&gt; filteredList =
        aList.stream()
            .filter(Predicate.not(a -&gt; bList.stream().anyMatch(b -&gt; a.getId().equals(b.getId()) &amp;&amp; a.getMobile().equals(b.getMobile()))))
            .collect(Collectors.toList());

For in place replacement

aList.removeIf(a -&gt; bList.stream().anyMatch(b -&gt; a.getId().equals(b.getId()) &amp;&amp; a.getMobile().equals(b.getMobile())));   

huangapple
  • 本文由 发表于 2020年8月15日 18:48:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63425167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定