public class Main {
private final static Pattern pattern = Pattern.compile("(?<left>\\d+)\\s(?<right>\\d+)");
public static void main(String[] args) throws Exception {
var inputFile = "in.txt";
var outputFile = "out.txt";
Files.lines(Paths.get(inputFile))
.limit(5) // Limit if needed
.map(Main::parseToResult)
.filter(Optional::isPresent)
.map(Optional::get)
.reduce((identity, accumulator) -> new Result(identity.left + accumulator.left,
identity.right + accumulator.right))
.map(Result::getResultAsString)
.ifPresent(s -> {
try (PrintWriter printWriter = new PrintWriter(outputFile)) {
printWriter.write(s);
} catch (Exception ignored) {/*NooP*/}
});
}
private static Optional<Result> parseToResult(String line) {
var matcher = pattern.matcher(line);
if (matcher.find()) {
var left = Integer.valueOf(matcher.group("left"));
var right = Integer.valueOf(matcher.group("right"));
return Optional.of(new Result(left, right));
} else {
return Optional.empty();
}
}
private record Result(Integer left, Integer right) {
String getResultAsString() {
if (left > right) {
return "1";
} else if (left < right) {
return "2";
} else {
return "draw";
}
}
}
}
public static void main(String[] args) {
Predicate<Integer> evens = i -> i % 2 == 0; // остаются только четные
var integers = IntStream.range(0, 10)
.boxed()
.filter(evens)
.peek(System.out::println)
.toList();
}
Точно нужно разбираться в БД и как в БД складывать и доставать объекты.