All in all, your approach is great. However, you can do this with a single-stream cascade . Compared to your original approach, this saves one iteration .
, , API - Javas, NIO. , Stream
. , Files#lines
, , .
, , :
String file = ...
Pattern separator = Pattern.compile("\\s");
try (Stream<String> lines = Files.lines(Paths.get(file))) {
T[] values = lines
.flatMap(separator::splitAsStream)
.mapToInt(Integer::parseInt)
.mapToObj(T::new)
.toArray(T[]::new);
} catch (IOException e) {
System.out.println("Something went wrong.");
}
, , . , :
List<T[]> valuesPerLine = Files.lines(Paths.get(file))
.map(separator::splitAsStream)
.map(lineStream -> {
return lineStream
.mapToInt(Integer::parseInt)
.mapToObj(T::new)
.toArray(T[]::new);
})
.collect(Collectors.toList());
, IntStream
Stream<T>
mapToObj(T::new)
( map
, , IntStream
), . Stream<T>
toArray(T[]::new)
.