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

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

使用流處理異常

使用流處理異常

白板的微信 2023-09-20 14:38:17
我有一個Map<String,List<String>>并希望它變成Map<String,List<Long>>因為String列表中的每個代表一個Long:Map<String,List<String>> input = ...;Map<String,List<Long>> output= input.entrySet()       .stream()       .collect(toMap(Entry::getKey, e -> e.getValue().stream()                                                      .map(Long::valueOf)                                                      .collect(toList()))               );我的主要問題是每個都String可能無法正確代表Long; 可能有一些問題。Long::valueOf可能會引發異常。如果是這種情況,我想返回 null 或空Map<String,List<Long>>因為我想迭代這張output地圖。但我不能接受任何錯誤的轉換;連一個也沒有。知道如何在 String -> Long 轉換不正確的情況下返回空輸出嗎?
查看完整描述

4 回答

?
慕娘9325324

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

我個人喜歡提供Optional有關數字解析的輸入:


public static Optional<Long> parseLong(String input) {

? ? try {

? ? ? ? return Optional.of(Long.parseLong(input));

? ? } catch (NumberFormatException ex) {

? ? ? ? return Optional.empty();

? ? }

}

然后,使用您自己的代碼(并忽略錯誤的輸入):


Map<String,List<String>> input = ...;

Map<String,List<Long>> output=?

input.entrySet()

? ? ? ?.stream()

? ? ? ?.collect(toMap(Entry::getKey, e -> e.getValue().stream()

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .map(MyClass::parseLong)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .filter(Optional::isPresent)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .map(Optional::get)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .collect(toList()))

? ? ? ? ? ? ? ?);

此外,考慮一個輔助方法來使其更加簡潔:


public static List<Long> convertList(List<String> input) {

? ? return input.stream()

? ? ? ? .map(MyClass::parseLong).filter(Optional::isPresent).map(Optional::get)

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

}


public static List<Long> convertEntry(Map.Entry<String, List<String>> entry) {

? ? return MyClass.convertList(entry.getValue());

}

然后您可以在流收集器中過濾結果:


Map<String, List<Long>> converted = input.entrySet().stream()

? ? .collect(Collectors.toMap(Entry::getKey, MyClass::convertEntry));

您還可以將空Optional對象保留在列表中,然后通過將新對象List<Optional<Long>>(而不是List<Long>)中的索引與原始對象進行比較List<String>,您可以找到導致任何錯誤輸入的字符串。您也可以簡單地將這些失敗記錄在MyClass#parseLong


但是,如果您的愿望是根本不對任何不良輸入進行操作,那么我將采取的路線是圍繞您試圖捕獲的整個流。


查看完整回答
反對 回復 2023-09-20
?
守著星空守著你

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

顯式地處理異常怎么樣catch:


private Map<String, List<Long>> transformInput(Map<String, List<String>> input) {

    try {

        return input.entrySet()

                .stream()

                .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().stream()

                        .map(Long::valueOf)

                        .collect(Collectors.toList())));

    } catch (NumberFormatException nfe) {

        // log the cause

        return Collections.emptyMap();

    }

}


查看完整回答
反對 回復 2023-09-20
?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

您可以創建一個StringBuilderfor 鍵(但有例外)并檢查是否ele為數字,如下所示,


 public static Map<String, List<Long>> transformInput(Map<String, List<String>> input) {

    StringBuilder sb = new StringBuilder();

    try {

    return input.entrySet()

            .stream()

            .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().stream()

                    .map(ele->{

                        if (!StringUtils.isNumeric(ele)) {

                            sb.append(e.getKey()); //add exception key

                            throw new NumberFormatException();

                        }

                        return Long.valueOf(ele);

                    })

                    .collect(Collectors.toList())));

} catch (NumberFormatException nfe) {

    System.out.println("Exception key "+sb);

    return Collections.emptyMap();

}

}

希望能幫助到你。


查看完整回答
反對 回復 2023-09-20
?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

也許您可以編寫一個輔助方法,該方法可以檢查字符串中的數字并從流中過濾掉它們以及空值,然后最終收集到 Map 中。


// StringUtils.java

public static boolean isNumeric(String string) {

    try {

        Long.parseLong(string);

        return true;

    } catch(NumberFormatException e) {

        return false;

    }

}

這會照顧一切。


并在您的信息流中使用它。


Map<String, List<Long>> newMap = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> mapToLongValues(entry.getValue())));


public List<Long> mapToLongValues(List<String> strs) {

    return strs.stream()

        .filter(Objects::nonNull)

        .filter(StringUtils::isNumeric)

        .map(Long::valueOf)

        .collect(Collectors.toList());

}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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