Core Java

core21FunctionalProgramming

Path
pkg1core/core21FunctionalProgramming.java
Package
pkg1core
Study order
22
Run
Single-file source launch
Command
java pkg1core/core21FunctionalProgramming.java
Lesson
Back to the chapter

There is no in-browser runner. This is the file from the curriculum, unchanged.

pkg1core/core21FunctionalProgramming.java
1package pkg1core;2 3/*4 * core21FunctionalProgramming.java5 * --------------------------6 * Lambdas, method references, and the core java.util.function interfaces.7 *8 * EXPLANATION:9 *  - A lambda implements a functional interface (exactly one abstract method).10 *  - Method references (Class::method) are shorthand for simple lambdas.11 *  - Core interfaces: Supplier, Consumer, Function, Predicate, BiFunction, etc.12 */13import java.util.*;14import java.util.function.*;15 16public class core21FunctionalProgramming {17 18    @FunctionalInterface19    interface Calculator { int op(int a, int b); }   // custom functional interface20 21    public static void main(String[] args) {22        // Custom functional interface via lambda23        Calculator add = (a, b) -> a + b;24        Calculator mul = (a, b) -> a * b;25        System.out.println("add=" + add.op(3, 4) + " mul=" + mul.op(3, 4));26 27        // Supplier: produces a value28        Supplier<String> greet = () -> "hello";29        System.out.println("Supplier: " + greet.get());30 31        // Consumer: accepts and acts32        Consumer<String> printer = s -> System.out.println("Consumer: " + s);33        printer.accept("consume me");34 35        // Function: transforms T -> R, with composition36        Function<Integer, Integer> doubler = x -> x * 2;37        Function<Integer, Integer> inc = x -> x + 1;38        System.out.println("compose (double then inc)(5) = " + doubler.andThen(inc).apply(5));39        System.out.println("compose (inc then double)(5) = " + doubler.compose(inc).apply(5));40 41        // Predicate: boolean test, with combinators42        Predicate<Integer> isEven = x -> x % 2 == 0;43        Predicate<Integer> isPositive = x -> x > 0;44        System.out.println("isEven.and(isPositive)(4) = " + isEven.and(isPositive).test(4));45        System.out.println("isEven.negate()(3) = " + isEven.negate().test(3));46 47        // BiFunction48        BiFunction<Integer, Integer, Integer> power = (b, e) -> (int) Math.pow(b, e);49        System.out.println("2^10 = " + power.apply(2, 10));50 51        // Method references (4 kinds)52        Function<String, Integer> len = String::length;             // instance method of arg53        Supplier<ArrayList<String>> factory = ArrayList::new;       // constructor ref54        Function<String, String> upper = String::toUpperCase;       // instance method55        BiFunction<String, String, Boolean> eq = String::equals;    // bound style56        System.out.println("len(hello)=" + len.apply("hello") + " upper=" + upper.apply("hi"));57        System.out.println("new list empty? " + factory.get().isEmpty() + " eq=" + eq.apply("a", "a"));58 59        // Using lambdas with collections60        List<Integer> nums = new ArrayList<>(List.of(5, 3, 1, 4, 2));61        nums.sort(Comparator.reverseOrder());62        nums.forEach(n -> System.out.print(n + " "));63        System.out.println();64    }65}