私はファイルを読み込み、整数リストを作成しています。文字列のリスト内の非整数のフィルタリング
サンプルファイル:「ABC」の整数に変換することができないので、それが失敗した以下のコードを実行するには
1 1 2 3 4
2 2 5 abc
4 2 8
。
非整数フィールドをJava 8 Egのよりクリーンな方法でフィルタリングすることが可能かどうか教えてください。
try (BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)))) {
List<Integer> allValues = new ArrayList<>();
br.lines().forEach(
strLine -> {
List<String> wordsList = Arrays.asList(strLine.trim().split(" "));
List<Integer> routes = wordsList.stream()
.filter(e -> e != null && !e.isEmpty())
.map(Integer::valueOf)
.collect(Collectors.toList());
allValues.addAll(routes);
});
allValues.forEach(str -> System.out.print(str));
}