JAVA — CHAPTER 14

Functional Programming with Lambdas & Streams · Cheat Sheet
Lambdas map / filter / reduce Method References Functional Interfaces Infinite Streams
1 STREAMS & THE PIPELINE MODEL
Source
list.stream()
Intermediate ops
filter, map (lazy)
Terminal op
collect, reduce, forEach
Result

A Stream is a sequence of elements you process using a pipeline of operations, instead of writing manual loops. Intermediate operations are lazy — nothing runs until a terminal operation triggers the whole pipeline, element by element.

2 LAMBDAS & MAPPING
List<String> names = List.of("amit","riya","sam"); List<String> upper = names.stream() .map(n -> n.toUpperCase()) // lambda expression .collect(Collectors.toList()); // [AMIT, RIYA, SAM]

Lambda syntax

  • (params) -> expression or (params) -> { statements; }
  • A lambda is a compact way to implement a functional interface (one abstract method) inline
  • map() transforms each element into something new
3 FILTERING & REDUCTION
List<Integer> nums = List.of(1,2,3,4,5,6); int sumOfEvens = nums.stream() .filter(n -> n % 2 == 0) // keep only evens .reduce(0, (a,b) -> a+b); // combine into one value // sumOfEvens = 12

Common terminal ops

  • reduce() — combines all elements into one result
  • count(), sum(), average()
  • collect(Collectors.toList()) — gather into a collection
4 METHOD REFERENCES & IntStream

Method references (::)

names.stream().map(String::toUpperCase); // shorthand for: n -> n.toUpperCase()

IntStream

IntStream.rangeClosed(1, 5) .forEach(System.out::println); // avoids boxing overhead of Stream<Integer>
5 FUNCTIONAL INTERFACES

Predicate<T>

test(T t) → boolean
used by filter()

Function<T,R>

apply(T t) → R
used by map()

Consumer<T>

accept(T t) → void
used by forEach()

Supplier<T>

get() → T
produces a value, no input

6 Stream<Employee> — A REALISTIC EXAMPLE
List<Employee> highEarners = employees.stream() .filter(e -> e.getSalary() > 50000) .sorted(Comparator.comparing(Employee::getSalary).reversed()) .collect(Collectors.toList()); double avgSalary = employees.stream() .mapToDouble(Employee::getSalary) .average() .orElse(0);
7 STREAMS OF RANDOM VALUES & INFINITE STREAMS
// bounded random stream new Random().ints(5, 1, 7).forEach(System.out::println); // infinite stream — must be limited! Stream.iterate(1, n -> n * 2) .limit(5) .forEach(System.out::println); // 1 2 4 8 16

Important

An infinite stream (Stream.iterate, Stream.generate) has no natural end — always pair it with limit(n) or it will never terminate.

8 METHOD REFERENCE TABLE — interface Stream<T>
Intermediate (lazy)Purpose
filter(predicate)keeps elements matching a condition
map(function)transforms each element
sorted()sorts elements
distinct()removes duplicates
limit(n)keeps only the first n
skip(n)skips the first n
Terminal (triggers execution)Purpose
forEach(consumer)performs an action on each element
collect(collector)gathers results into a List/Set/Map
reduce(identity, op)combines all elements into one value
count()number of elements
anyMatch/allMatch/noneMatch(p)boolean tests across elements
findFirst()an Optional with the first element