簡體   English   中英

如何比較兩個不同大小的列表對象 Java

[英]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中刪除與ListB具有相同手機號碼的相應 ID。

試過下面的代碼

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;
}

有人可以指導我嗎?

您的方法創建一個新列表,而不是從現有列表中刪除項目。 假設您確實想要刪除項目,這是您可以使用 Java 8 流 API 執行此操作的一種方法:如果項目與列表 B 中的項目具有相同的mobile ,則從列表 A 中刪除該項目:

listA.removeIf(a -> listB.stream()
                         .anyMatch(b -> Objects.equals(a.getMobile(), b.getMobile())));

在這種情況下,流 API 有點難以閱讀。 不使用 stream 也是一樣的:

for (B b : listB) {
    listA.removeIf(a -> Objects.equals(a.mobile, b.mobile));
}

您可以使用標志表示存在並在 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);
    }
}

注意:有問題你說只比較手機號碼,但在代碼中你也在比較 id,如果你不想要它,請刪除 id equal 檢查

我有兩個大小不同的列表,列表 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;
}

現在我想比較兩個列表,並希望從List A中刪除與ListB具有相同手機號碼的各個 ID。

試過下面的代碼

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;
}

有人可以指導我嗎?

我認為您應該將手機分開存放,因為它們可能會重復使用。 然后將主要集合與此手機列表進行比較。

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());
}

創建新的過濾列表

如果條件不匹配,則收集新列表中的所有元素

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())));   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM