There are several ways to do this. The counter technique proposed by Saloparenator's answer can be implemented as follows, using AtomicIntegeras a mutable counter object and assuming an obvious class Pair:
List<Pair> getPairs1() throws IOException {
AtomicInteger counter = new AtomicInteger(0);
try (Stream<String> lines = Files.lines(Paths.get(FILENAME))) {
return lines.parallel()
.map(line -> new Pair(line, counter.incrementAndGet()))
.collect(toList());
}
}
, , , . , . Files.lines , , incrementAndGet(). , . , , , , .
. , , . :
static List<Pair> getPairs2() throws IOException {
List<String> lines = Files.readAllLines(Paths.get(FILENAME));
return IntStream.range(0, lines.size())
.parallel()
.mapToObj(i -> new Pair(lines.get(i), i+1))
.collect(toList());
}