簡體   English   中英

檢查兩個對象列表是否具有相同的屬性值

[英]check if two lists of objects have the same value of an attribute

我有兩個對象列表localListremotList ,兩個列表都有一個同意屬性

如果兩個列表相同,我想檢查同意屬性的值

如果不是,我想從 localList 中刪除所有對象不具有 remotList 中存在的相同同意值,並添加所有對象具有 remotList 而不是 localList 中存在的相同同意值

我實施了這個解決方案,但我想改進它

Java 示例

    class Customer{

    private Long id;
    private String name;
    private Integer consent;

    public Customer(Long id, String name, Integer consent) {
        this.id = id;
        this.name = name;
        this.consent = consent;
    }

    public Integer getConsent() {
        return consent;
    }

    @java.lang.Override
    public java.lang.String toString() {
        return "Customer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", consent=" + consent +
                '}';
    }
}

 public static void main(String[] args) {

    List<Customer> localList = new ArrayList<>();
    localList.add(new Customer(1L, "name1", 12));
    localList.add(new Customer(2L, "name2", 13));
    localList.add(new Customer(3L, "name3", 14));
    localList.add(new Customer(4L, "name4", 15));

    List<Customer> remoteList = new ArrayList<>();
    remoteList.add(new Customer(10L, "name1", 12));
    remoteList.add(new Customer(11L, "name2", 11));
    remoteList.add(new Customer(12L, "name3", 14));
    remoteList.add(new Customer(13L, "name4", 16));

    Map<Integer, Customer> map = remoteList.stream()
            .collect(Collectors.toMap(s -> s.getConsent() , s -> s));
    Map<Integer, Customer> map2 = localList.stream()
            .collect(Collectors.toMap(s -> s.getConsent() , s -> s));

      List<Customer> remove = new ArrayList<>();

    localList.forEach(e -> {
        if(map.get(e.getConsent()) == null ) {
            remoteList.add(e);
        }
    });

    remoteList.forEach(e -> {
        if(map2.get(e.getConsent()) == null ) {
            remove.add(e);
        }
    });

    remove.forEach(e ->  remoteList.remove(e));

    remoteList.forEach(System.out::println);

遠程列表

Customer{id=10, name='name1', consent=12}
Customer{id=11, name='name2', consent=11}
Customer{id=12, name='name3', consent=14}
Customer{id=13, name='name4', consent=16}

本地列表

Customer{id=1, name='name1', consent=12}
Customer{id=2, name='name2', consent=13}
Customer{id=3, name='name3', consent=14}
Customer{id=4, name='name4', consent=15}

結果

Customer{id=10, name='name1', consent=12}
Customer{id=12, name='name3', consent=14}
Customer{id=2, name='name2', consent=13}
Customer{id=4, name='name4', consent=15}

對於第一部分,您可以為remoteList創建一個 map,按name映射每個客戶。 我們這樣做是為了能夠更快地搜索遠程客戶。

Map<String, Customer> customerByName = remoteList.stream()
                .collect(Collectors.toMap(Customer::getName, customer -> customer));

localList.removeIf(customer -> !Objects.equals(customer.getConsent(),
                customerByName.get(customer.getName()).getConsent()));

請注意, Customer class 需要一個 getter 作為name

對於第二部分,我們還可以像之前所做的那樣為localList創建一個 map。 我們遍歷remoteList並過濾localList中不存在的每個元素(至少我相信這是您想要完成的)。

Map<String, Customer> localCustomerByName = localList.stream()
                .collect(Collectors.toMap(Customer::getName, customer -> customer));

localList.addAll(remoteList.stream()
                .filter(customer -> !localCustomerByName.containsKey(customer.getName()))
                .collect(Collectors.toList()));
Map<Integer, Customer> map = remoteList.stream()
            .collect(Collectors.toMap(s -> s.getConsent() , s -> s));
List<Customer> hasDiffLastItem = localList.stream()
                .filter(s -> !map.get(getConsent).getConsent().equals(s.getConsent()))
                .collect(Collectors.toList());

這是一種方法。 我使用了鏈表,因為它們在刪除項目方面更有效。

  • 首先,為一組創建一個比較器。 樹集將采用比較器代替 equals 實現。
  • 然后遍歷遠程列表,如果項目出現在本地集合中,則刪除它們。
  • 然后遍歷本地列表,如果項目沒有出現在遠程集中,則刪除它們。
Set<Customer> remoteSet = new TreeSet<>(comp);
Set<Customer> localSet = new TreeSet<>(comp);
remoteSet.addAll(remoteList);
localSet.addAll(localList);

Iterator<Customer> iter = localList.iterator();
while(iter.hasNext()) {
    Customer s = iter.next();
    if (remoteSet.contains(s)) {
        iter.remove();
    }
}
iter = remoteList.iterator();
while(iter.hasNext()) {
    Customer s = iter.next();
    if (!localSet.contains(s)) {
        iter.remove();
    }
}

您可以根據需要組合列表,但這里是背靠背的結果。

localList.forEach(System.out::println);
remoteList.forEach(System.out::println);

印刷

Customer{id=2, name='name2', consent=13}
Customer{id=4, name='name4', consent=15}
Customer{id=10, name='name1', consent=12}
Customer{id=12, name='name3', consent=14}

我使用您和我的方法嘗試了不同的值,output 是相同的。 我建議您自己檢查一下,因為我不完全確定您希望它如何工作。

暫無
暫無

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

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