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

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

使用流優化列表遍歷

使用流優化列表遍歷

繁花如伊 2023-02-23 17:41:19
我有一個List<BatchDTO>與以下類public class BatchDTO {    private String batchNumber;    private Double quantity;..//Getters and setters}如果 batchNumber 重復,我要做的是總結總數。我使用 LinkedHashMap 來實現它,并進行了迭代。但我想要的是一種更優化的方式。我可以使用流以優化的方式執行此操作嗎?private static List<BatchDTO > getBatchDTO (Map<String, BatchDTO > batchmap) {    return batchmap.values().stream().collect(Collectors.toList());}private static Map<String, BatchDTO > getBatchMap(List<BatchDTO > batchList, Map<String, BatchDTO > batchMap) {        for (BatchDTO  batchDTO  : batchList) {            batchMap = getBatchMap(batchMap, batchDTO );        }    return batchMap;}private static Map<String, BatchDTO > getBatchMap(Map<String, BatchDTO > batchMap, BatchDTO  batchObject) {    String batchCode = batchObject.getBatchNumber();        if(!batchMap.containsKey(batchCode)) {            batchMap.put(batchCode, batchObject);        } else {            batchObject.setQuantity(getTotalQuantity(batchMap,batchObject));            batchMap.put(batchCode, batchObject);        }    return batchMap;}private static Double getTotalQuantity(Map<String, BatchDTO > batchmap, BatchDTO  batchObject) {    return batchmap.get(batchObject.getBatchNumber()).getQuantity() + batchObject.getQuantity();}
查看完整描述

2 回答

?
九州編程

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

假設BatchDTO擁有所有 args 構造函數,您可以從返回MapList<BatchDTO>

List<BatchDTO>?collect?=?list.stream()
????????.collect(groupingBy(BatchDTO::getBatchNumber,?summingDouble(BatchDTO::getQuantity)))
????????.entrySet().stream()
????????.map(entry?->?new?BatchDTO(entry.getKey(),?entry.getValue()))
????????.collect(Collectors.toList());

JavaDoc:?groupingBy()?,?summingDouble()


查看完整回答
反對 回復 2023-02-23
?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

代碼中的注釋可能有點難以理解,但這就是我的全部時間。


// Result will be a Map where the keys are the unique 'batchNumber's, and the

// values are the sum of the 'quantities' for those with that 'batchNumber'.

public Map<String, Double> countBatchQuantities(final List<BatchDTO> batches) {

    // Stream over all the batches...

    return batches.stream()


    // Group them by 'batch number' (gives a Map<String, List<BatchDTO>>)

            .collect(Collectors.groupingBy(BatchDTO::getBatchNumber))


    // Stream over all the entries in that Map (gives Stream<Map.Entry<String, List<BatchDTO>>>)

            .entrySet().stream()


    // Build a map from the Stream of entries

    // Keys stay the same

            .collect(Collectors.toMap(Entry::getKey, 


    // Values are now the result of streaming the List<BatchDTO> and summing 'getQuantity'

                    entry -> entry.getValue().stream().mapToDouble(BatchDTO::getQuantity).sum()));

}

注意:我不保證這比您現有的方法更優化......但它可以使用 Streams 完成工作。quantity注意:如果是null針對您的任何一個,這將引發異常BatchDTO...


查看完整回答
反對 回復 2023-02-23
  • 2 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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