Functional basics
On this page 8 topics
Overview
Topics
- Lambda expressions
- functional interfaces
- method references
Learn first
54 min of reading · 2.5 h of practice (estimate)
Learning outcomes
- Write lambda expressions and explain them as implementations of a functional interface.
- Use
Predicate,Function,Consumer,Supplier, andBiFunction. - Replace lambdas with method references where clearer.
Lessons
Lab and exercises
Lab
BEG-035-LAB1 · Filters
Outcomes: 1–3 · Time: ~1.5 h Rewrite anonymous Comparator classes and custom filter loops using lambdas and method references. Keep a before/after copy. Acceptance: same behavior, less code.
Exercises (assessment: four exercises)
| ID | Outcome | Task |
|---|---|---|
| BEG-035-EX01 | 1 | Convert an anonymous Runnable that prints "hello" into a lambda; explain which interface it implements. |
| BEG-035-EX02 | 2 | Write one lambda for each: Predicate<Integer> (even), Function<String,Integer> (length), Consumer<String> (print upper-case), Supplier<String> ("ready"), BiFunction<Integer,Integer,Integer> (max). |
| BEG-035-EX03 | 3 | Rewrite as method references: s -> s.toUpperCase(), s -> System.out.println(s), () -> new ArrayList<String>(). |
| BEG-035-EX04 | 1 | Write filter(List<Integer>, Predicate<Integer>) returning a new list; use it for positives and for numbers over 100. |
Show solutions
- EX01
Runnable r = () -> System.out.println("hello");It implementsRunnable, whose single method isrun. - EX02
n -> n % 2 == 0;s -> s.length();s -> System.out.println(s.toUpperCase());() -> "ready";(a, b) -> Math.max(a, b). - EX03
String::toUpperCase;System.out::println;ArrayList::new(asSupplier<List<String>>). - EX04 Loop,
if (p.test(x)) result.add(x);. Calls:filter(list, n -> n > 0),filter(list, n -> n > 100). - Mistakes (long lambdas, captured variables) are covered in L02 and demo L05.
Quiz
Outcomes: O1 lambdas and functional interfaces · O2 standard interfaces · O3 method references.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Four exercises BEG-035-EX01 to EX04 plus the lab. Pass: all four exercises correct and the lab behaving the same as before with less code.
Common mistakes
Multi-line lambdas that should be methods; modifying captured variables.
Assessment
4 exercises.
Career relevance
Streams (BEG-036), CompletableFuture (MID-006), Spring configuration.