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

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

如何在列表中查找重復項并合并它們

如何在列表中查找重復項并合并它們

慕森卡 2023-08-09 15:12:56
我有一個可能重復的列表。我通過 ID 識別重復項。這些對象有子對象,現在我想合并重復項,以便子對象僅附加到一個對象。我如何最好地識別重復項,也許是通過流?public class Foo {  private String id;  private Collection<String> childs;}  private Collection<Foo> mergeDuplicates(Collection<Foo> fooList) {    /*this method should call the mergeChilds on found Duplicates,    and return the processed Collection of Foos*/  }  private Foo mergeChilds(Foo foo1, Foo foo2) {    ...  }
查看完整描述

3 回答

?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

您可以將它們收集到Map基于id并children使用合并mergeFunction。然后將它們映射回最終對象,如下所示:


private Collection<Foo> mergeDuplicates(Collection<Foo> fooCollection) {

    return fooCollection.stream()

            .collect(Collectors.toMap(Foo::getId, Foo::getChildren, this::mergeChildren))

            .entrySet().stream()

            .map(e -> new Foo(e.getKey(), e.getValue()))

            .collect(Collectors.toCollection(ArrayList::new)); // collect accordingly

}

更新后的mergeChildren方法在同一個類中實現:


private Collection<String> mergeChildren(Collection<String> foo1Children, Collection<String> foo2Children) {

    foo1Children.addAll(foo2Children);

    return foo1Children;

}

注意:僅當識別出基于的重復項時才執行mergeFunction ( )。(a,b) -> {...}id


查看完整回答
反對 回復 2023-08-09
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

映射并重新連接子級:


List<Obj> list = ...;

Map<Long, Obj> objectsById = new HashMap<>();

list.forEach(obj -> {

    objectsById.merge(obj.getId(), obj,

        (oldv, v) -> {

            if (oldv != null) {

                v.getChildren().forEach(ch -> ch.setParent(oldv));

                return oldv;

            }

            return v;

    });

});

list = objectsById.values();

如果只有 getParent,沒有 getChildren?;蛘咦訉ο笠彩歉笇ο?,則需要進行第二次行走以從子對象中刪除過時的對象(未出現在地圖中)。


查看完整回答
反對 回復 2023-08-09
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

這是針對您的用例的詳細示例,希望對您有所幫助。這使用流查找重復項,然后將子項附加到現有對象。


import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

import java.util.stream.Collectors;


public class HelloWorld {

? ? public static void main(String[] args) {

? ? ? ? Pojo p1 = new Pojo("a", new ArrayList<String>(Arrays.asList("c1", "c2")));

? ? ? ? Pojo p2 = new Pojo("a", new ArrayList<String>(Arrays.asList("c3", "c4")));

? ? ? ? Pojo p3 = new Pojo("b", new ArrayList<String>(Arrays.asList("c5", "c6")));


? ? ? ? List<Pojo> pojos = new ArrayList<Pojo>();

? ? ? ? pojos.add(p1);

? ? ? ? pojos.add(p2);

? ? ? ? pojos.add(p3);


? ? ? ? Set<Pojo> uniquePojos = new HashSet<>();


? ? ? ? pojos.stream().filter(p -> {

? ? ? ? ? ? boolean notExists = uniquePojos.add(p);

? ? ? ? ? ? if (!notExists) {

? ? ? ? ? ? ? ? for (Pojo up : uniquePojos) {

? ? ? ? ? ? ? ? ? ? if (up.equals(p)) {

? ? ? ? ? ? ? ? ? ? ? ? up.children.addAll(p.children);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? return notExists;

? ? ? ? }).collect(Collectors.toList());


? ? ? ? System.out.println(uniquePojos);


? ? }

}


class Pojo {


? ? Pojo(String id, List<String> children) {

? ? ? ? this.id = id;

? ? ? ? this.children = children;

? ? }


? ? String id;

? ? List<String> children;


? ? @Override

? ? public int hashCode() {

? ? ? ? final int prime = 31;

? ? ? ? int result = 1;

? ? ? ? result = prime * result + ((id == null) ? 0 : id.hashCode());

? ? ? ? return result;

? ? }


? ? @Override

? ? public boolean equals(Object obj) {

? ? ? ? if (this == obj)

? ? ? ? ? ? return true;

? ? ? ? if (obj == null)

? ? ? ? ? ? return false;

? ? ? ? if (getClass() != obj.getClass())

? ? ? ? ? ? return false;

? ? ? ? Pojo other = (Pojo) obj;

? ? ? ? if (id == null) {

? ? ? ? ? ? if (other.id != null)

? ? ? ? ? ? ? ? return false;

? ? ? ? } else if (!id.equals(other.id))

? ? ? ? ? ? return false;

? ? ? ? return true;

? ? }


? ? @Override

? ? public String toString() {

? ? ? ? return "Pojo [id=" + id + ", children=" + children.toString() + "]";

? ? }


}


結果:


[Pojo [id=a, children=[c1, c2, c3, c4]], Pojo [id=b, children=[c5, c6]]]


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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