Created
March 29, 2017 22:17
-
-
Save rayhon1014/a07e048245ead84b27c634278a7b5889 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> strList = Stream.of("1", "2", "illegal_3", "4", "illegal_5", "6").collect(Collectors.toList()); | |
//return null and filter null | |
intList = strList.stream()// same with "parallelStream()" | |
.map(x -> { | |
try { | |
return Integer.parseInt(x); | |
} catch (NumberFormatException nfe) { | |
System.err.println(nfe.getMessage()); | |
} | |
return null; | |
}) | |
.filter(x -> x!= null) | |
.collect(Collectors.toList()); | |
intList = strList.stream() | |
.flatMap(x -> parseIntStream(x)) | |
.collect(Collectors.toList()); | |
//return empty stream if error | |
static Stream<Integer> parseIntStream(String s) { | |
try { | |
return Stream.of(Integer.parseInt(s)); | |
} catch (NumberFormatException nfe) { | |
System.err.println(nfe.getMessage()); | |
} | |
return Stream.empty(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment