Технологии Java
Лямбда-выражения и потоки
BiFunction<String, Long, String> f = (String s, Long v) −> { return s + v; }
BiFunction<String, Long, String> f = (String s, Long v) −> s + v;
BiFunction<String, Long, String> f = (s, v) −> s + v;
Function<String, String> f = s −> s + s;
Supplier<String> s = () −> "!";
Consumer<String> c = s −> System.out.println(s + s);
default <V> Function<V, R> compose(Function<−V, +T> before) { return v −> apply(before.apply(v)); }
default <V> Function<T, V> andThen(Function<−R, +V> after) { return v −> after.apply(apply(v)); }
Function<String, Integer> f1 = Integer::parseInt; // x −> Integer.parseInt(x);
Function<Integer, String> f2 = Integer::toString; // x −> x.toString();
Integer i = 2; // Не обязательно final Supplier<String> f3 = i::toString; // () −> i.toString();
Function<String, Integer> f = Integer::new; // s −> new Integer(s);
/* final */ String hello = "Hello, "; Function<String, String> greeter = name −> hello + name;
// Не работает, так как write бросает IOException Consumer<String> c = writer::write;
collection.forEach(e −> { if (e.equals("done")) { // Что делать? } });
@FunctionalInterface interface IOSupplier<T> { T get() throws IOException; static <T> T unchecked(IOSupplier<T> supplier) { try { return supplier.get(); } catch (IOException e) { throw new UncheckedIOException(e); } } }
List<String> lines = IOSupplier.unchecked(() −> Files.readAllLines(Path.of("input.txt")));
courses.sort( Comparator.comparingInt(Course::getYear) .thenComparingInt(Course::getTerm) .thenComparing( Course::getName, String.CASE_INSENSITIVE_ORDER ))
courses.forEach(System.out::println);
courses.removeIf(Course::isDone);
courses.toArray(Course[]::new);
courses.replaceAll(Course::nextYear);
Map<String, Integer> map = new HashMap<>(); while (scanner.hasNext()) { map.merge(scanner.next(), 1, Integer::sum); }
Map<String, Integer> map = new HashMap<>(); Files.readAllLines(input).forEach(line −> { for (String word : line.split("\\s+")) { map.merge(word, 1, Integer::sum); } });
string.stream() .filter(s −> s.endsWith("s")) .mapToInt(String::length) .min();
string.parallelStream() .filter(s −> s.contains("a")) .sorted(String.CASE_INSENSITIVE_ORDER) .limit(3) .collect(Collectors.joining(", "));
Map<String, Long> map = words.stream() .collect(Collectors.groupingBy( String::toUpperCase, Collectors.counting() ));
Map<String, Long> map = Files.lines(path) .flatMap(line −> Arrays.stream(line.split("\\s+")) .collect(Collectors.groupingBy( String::toUpperCase, Collectors.counting() ));
words.stream() .collect(Collectors.joining(", ", "{", "}"));
Collector<Student, String[], Map.Entry<String, String>> MIN_NAMES = Collector.of( () −> new String[2], (mins, Student) −> { mins[0] = min(mins[0], Student.getFirstName()); mins[1] = min(mins[1], Student.getLastName()); }, (mins1, mins2) −> { mins1[0] = min(mins1[0], mins2[0]); mins1[1] = min(mins1[1], mins2[1]); return mins1; }, mins −> Map.entry(mins[0], mins[1]) );