4 回答

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

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();
}
}

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();
}
}
希望能幫助到你。

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());
}
添加回答
舉報