第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

java8 簡(jiǎn)化映射的轉(zhuǎn)換

java8 簡(jiǎn)化映射的轉(zhuǎn)換

藍(lán)山帝景 2022-07-27 21:41:36
我有這個(gè)類(lèi)結(jié)構(gòu):public class A {    private List<B> bs;...//getters}public class C {    private Long id;...//getters}public class B {    private Long idOfC;...//more stuff}B::getIdOfC 匹配 C::getId在更好的設(shè)計(jì)中,B 將只包含對(duì) C 的引用,而不是它的 id(我無(wú)法更改),所以這就是為什么現(xiàn)在我需要?jiǎng)?chuàng)建一個(gè) map,所以我的方法簽名看起來(lái)像這樣public Map<A, List<C>> convert(Collection<A> collection)在這個(gè)轉(zhuǎn)換方法里面,有一個(gè)List<C> getCsByIds(List<Long> id) 后來(lái)用于將其與 B.idOfC 匹配,但應(yīng)該只調(diào)用一次此方法,因?yàn)樗浅0嘿F。所以如果我這樣去: List<B> bs = Arrays.asList(new B(10L), new B(11L)); //10L and 11L are the values of idOfC   List<A> as = Arrays.asList(bs);   //And assuming getCsByIds returns Arrays.asList(new C(10L), new C(11L), new C(12L));然后    Map<A, List<C>> map = convert(as);    map.values().get(0) 返回類(lèi)似的東西Arrays.asList(new C(10L), new C(11L))在我看來(lái),這樣做的方法非常龐大:    public Map<A, List<C>> convert(Collection<A> as) {    List<Long> cIds = as.stream()            .flatMap(a -> a.getBs().stream())            .map(B::getCId)            .collect(Collectors.toList());    //single call to gsCsByIds    Map<Long, C> csMap = getCsByIds(cIds)            .stream()            .collect(Collectors.toMap(C::getId, Function.identity()));    //a whole new map is created by iterating over the list called "as"    Map<A, List<C>> csByAs = new HashMap<>();    if (!csMap.isEmpty()) {        for (A a : as) {            Set<C> cs = getCsFromMap(csMap, a.getBs());            if (!cs.isEmpty()) {                csByAs.put(a, new ArrayList<>(cs));            }        }    }    return csByAs;}private Set<B> getCsFromMap(Map<Long, C> cMap, List<B> bs) {    return bs.stream()            .map(b -> cMap.get(b.getIdOfc()))            .collect(Collectors.toSet());}有沒(méi)有辦法讓這個(gè)更簡(jiǎn)單???
查看完整描述

3 回答

?
開(kāi)滿(mǎn)天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊

如果調(diào)用getCsByIds成本很高,那么您最初的想法很適合自己執(zhí)行。它可以進(jìn)一步縮短為:


public Map<A, List<C>> convert(Collection<A> as) {

    List<Long> cIds = as.stream()

            .flatMap(a -> a.getBs().stream())

            .map(B::getIdOfC)

            .collect(Collectors.toList());

    Map<Long, C> csMap = getCsByIds(cIds).stream()

            .collect(Collectors.toMap(C::getId, Function.identity()));


    return as.stream()

            .collect(Collectors.toMap(Function.identity(),

                    a -> a.getBs().stream().map(b -> csMap.get(b.getIdOfC()))

                            .collect(Collectors.toList()), (a, b) -> b));

}

您可以在其中相應(yīng)地選擇合并功能(a,b) -> b。


查看完整回答
反對(duì) 回復(fù) 2022-07-27
?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

也許只是直接迭代 As ?(手頭沒(méi)有編譯器,所以片段可能沒(méi)有編譯就緒)


public Map<A, List<C>> convert(Collection<A> as) {

  Map<A, List<C>> result = new HashMap<>();

  for(A a: as){

     List<Long> cIds = a.getBs().stream()

                         .map(B::getIdOfC)

                         .collect(Collectors.toList());

     result.put(a, getCsByIds(cIds));

  }

  return result;

}


查看完整回答
反對(duì) 回復(fù) 2022-07-27
?
蝴蝶不菲

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

像這樣的東西不會(huì)起作用嗎?我沒(méi)有編譯器,所以我無(wú)法真正測(cè)試它


public Map<A, List<C>> convert(Collection<A> as) {

    return as.stream()

             .collect(Collectors.toMap(Function::identity,

                                       a -> a.getBs().stream()

                                                     .map(B::getIdOfC)

                                                     .flatMap(id -> getCsByIds(asList(id))

                                                                   .values()

                                                                   .stream())

                                                     .collect(Collectors.toList())

                                      )

                     );

}


查看完整回答
反對(duì) 回復(fù) 2022-07-27
  • 3 回答
  • 0 關(guān)注
  • 112 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)