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

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

在 Java 流中執(zhí)行歸約操作時出現(xiàn)異常

在 Java 流中執(zhí)行歸約操作時出現(xiàn)異常

阿晨1998 2022-07-27 21:42:12
我是 Java 8 的新手,在下面的示例中,我創(chuàng)建了一個 Map,其鍵值為 String,值為整數(shù)的 ArrayList。Map<String,List<Integer>> mapLstInteger=new HashMap<String,List<Integer>>() {            {                put("A",Arrays.asList(1,2,3));                put("B",Arrays.asList(4,5,6));                put("C",Arrays.asList(7,8,9));            }        };我編寫了下面的代碼來針對每個鍵執(zhí)行 arrayList 元素的總和,并試圖將總和值存儲在單獨的 ArrayList 中。List<Integer> sumLst=mapLstInteger.entrySet().stream().map(e->e.getValue())        .reduce((inputLst, outputLst)->{            int sum=0;            for(int count=0;count<inputLst.size();count++)            {                sum=sum+inputLst.get(count);            }            outputLst.add(sum);            return outputLst;        }).get();當我嘗試執(zhí)行以下代碼時,我遇到了異常。com.calculation.sum.client 的 java.util.AbstractList.add(AbstractList.java:108) 的 java.util.AbstractList.add(AbstractList.java:148) 的線程“main”java.lang.UnsupportedOperationException 中的異常。 Client.lambda$1(Client.java:43) at java.util.stream.ReduceOps$2ReducingSink.accept(ReduceOps.java:123) at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) at java.util.HashMap$EntrySpliterator.forEachRemaining(HashMap.java:1696) 在 java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) 在 java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)在 java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) 在 java.util.stream.java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)。ReferencePipeline.reduce(ReferencePipeline.java:479) 在 com.calculation.sum.client.Client.main(Client.java:37)任何人都可以讓我知道我在上面的代碼中做錯了什么>
查看完整描述

3 回答

?
哆啦的時光機

TA貢獻1779條經(jīng)驗 獲得超6個贊

首先,您使用Arrays::asList的是記錄為返回由指定數(shù)組支持的固定大小列表,我認為固定大小應(yīng)該告訴您您做錯了什么。

HashMap比您使用創(chuàng)建就地的反模式- 通過創(chuàng)建擴展的匿名內(nèi)部類HashMap,通過 that Map<String,List<Integer>> mapLstInteger=new HashMap<String,List<Integer>>()....。

比,你違反了 的規(guī)范reduce,它應(yīng)該一直返回一個新的對象,但你總是放入outputLst.

比,Map當你只關(guān)心它的值時,你正在創(chuàng)建一個 -List<List<Integer>>在這種情況下創(chuàng)建一個。

根據(jù)您的代碼,即使您在代碼下面編寫的用于針對每個鍵對 arrayList 元素求和的句子也不正確。如果我是你,我會在我想要實現(xiàn)的實際目標上下定決心,然后嘗試去做。


查看完整回答
反對 回復(fù) 2022-07-27
?
慕的地10843

TA貢獻1785條經(jīng)驗 獲得超8個贊

發(fā)生這種情況是因為您使用的AbstractList是由Arrays.asList.

該List<T>抽象實現(xiàn)不允許添加或刪除元素。


public void add(int index, E element) {

    throw new UnsupportedOperationException();

}

但無論如何,回到你的問題。您也可以通過 custom獲得您想要的東西Collector,您可以在其中提供您的自定義List<T>實現(xiàn),無論是ArrayList,LinkedList還是您覺得更好的任何東西。


mapLstInteger.values()

             .stream()

             .collect(Collector.of(

                     () -> new ArrayList<>(),  // Supplier

                     (output, toSumList) -> {  // Accumulator

                         output.add(toSumList.stream()

                                             .mapToInt(Integer::intValue)

                                             .sum());

                     },

                     // The Combiner implementation will be called

                     // in case of a "parallel" Stream. 

                     // No need to worry about it here. 

                     // But in case, we would need to merge the partial results

                     (output, partial) -> {

                        output.addAll(partial);

                        return output;

                     }

             ));

更簡潔的版本是


mapLstInteger.values()

             .stream()

             .map(l -> l.stream().mapToInt(Integer::intValue).sum())

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

這將正確輸出[6, 15, 24]


查看完整回答
反對 回復(fù) 2022-07-27
?
茅侃侃

TA貢獻1842條經(jīng)驗 獲得超21個贊

您應(yīng)該執(zhí)行以下操作:

    mapLstInteger.values().stream()
                 .flatMapToInt(list -> list.stream()
                                           .filter(Objects::nonNull)
                                           .mapToInt(Integer::intValue)).sum();

添加了過濾器以確保在空整數(shù)的情況下不會獲得空指針。作為一般規(guī)則,如果您被迫在流中使用常規(guī)循環(huán),您可能做錯了什么。通過將 int 列表更改為 int 值,我們可以輕松地求和,如上所示。

最初誤解了這個問題,認為您想要總和,唉,這是針對實際問題的更新解決方案:

    mapLstInteger.values().stream()
                 .map(list -> list.stream()
                                  .filter(Objects::nonNull)
                                  .mapToInt(Integer::intValue).sum())
                                  .collect(Collectors.toList());


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

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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