亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java 8 流 - 通過比較兩個列表進行過濾

Java 8 流 - 通過比較兩個列表進行過濾

搖曳的薔薇 2023-09-06 14:56:59
我有 2 個彼此不同的列表public class App1{    private String name;    private String city;    // getter setter    // constructors}public class App2{    private String differentName;    private String differentCity;    private String someProperty1;    private String someProperty2;    // getter setter    // constructors}List<App1> app1List = new ArrayList<>();app1List.add(new App1("test1","city1"));app1List.add(new App1("test2","city2"));app1List.add(new App1("test3","city3"));app1List.add(new App1("test4","city4"));List<App2> app2List = new ArrayList<>();app2List.add(new App2("test2","city2"));app2List.add(new App2("test3","city3"));如您所見,App1 和 App2 類是 2 個具有不同屬性名稱的不同 pojo,但是 name、city 和 differentName、 differentCity 屬性分別持有的內容/值是相同的,即 test1、test2、test3 和 city1、city2 等現在我需要過濾 app1List 比較其他列表中的名稱和城市,即不存在的 app2List。最終輸出將是app1List.add(new App1("test1","city1"));app1List.add(new App1("test4","city4"));最簡單的方法是多次循環其他列表之一,這是我試圖避免的。Java 8 流中有什么方法不必循環多次?
查看完整描述

3 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

您可以使用noneMatch操作,例如:


List<App1> result = app1List.stream()

        .filter(app1 -> app2List.stream()

                .noneMatch(app2 -> app2.getDifferentCity().equals(app1.getCity()) &&

                        app2.getDifferentName().equals(app1.getName())))

        .collect(Collectors.toList());

這假設兩者的組合name并且在 ingcity時匹配filter。


查看完整回答
反對 回復 2023-09-06
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

您需要override equals在類中使用方法App2:


public class App2{

    private String differentName;

    private String differentCity;

    private String someProperty1;

    private String someProperty2;


    // getter setter


    // constructors


    @Override

    public boolean equals(Object obj) {

       App2 app2 = (App2) obj;

       return this.differentName.equals(app2.getDifferentName()) && this.differentCity.equals(app2.getDifferentCity());

    }

}

然后您可以像這樣在 list1 上使用 Streams:


app1List = app1List.stream()

                .filter(a-> !app2List.contains(new App2(a.getName(),a.getCity())))

                .collect(Collectors.toList());

輸出:


[App1{name='test1', city='city1'}, App1{name='test4', city='city4'}]


查看完整回答
反對 回復 2023-09-06
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

假設您想要匹配名稱和城市,您可以創建一個將對象映射到key的函數,例如:


public static Integer key(String name, String differentCity) {

    return Objects.hash(name, differentCity);

}

然后使用該鍵創建一組鍵,以便使用noneMatch進行過濾,例如:


Set<Integer> sieve = app2List.stream()

        .map(app2 -> key(app2.differentName, app2.differentCity)).collect(Collectors.toSet());


List<App1> result = app1List.stream().filter(app1 -> sieve.stream()

        .noneMatch(i -> i.equals(key(app1.name, app1.city))))

        .collect(Collectors.toList());


System.out.println(result);

輸出


[App1{name='test1', city='city1'}, App1{name='test4', city='city4'}]

這種方法的復雜性在于O(n + m)其中n和m是列表的長度。


查看完整回答
反對 回復 2023-09-06
  • 3 回答
  • 0 關注
  • 217 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號