Skip to content
Ameen Abozaid
BEG-035

Functional basics

  • 5 Lessons
  • 54 min of reading
  • 2.5 h practice (estimate)
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

  1. Write lambda expressions and explain them as implementations of a functional interface.
  2. Use Predicate, Function, Consumer, Supplier, and BiFunction.
  3. Replace lambdas with method references where clearer.

Lessons

  1. 01Passing behavior as a value11 min read
  2. 02Lambda syntax12 min read
  3. 03The standard functional interfaces10 min read
  4. 04Method references10 min read
  5. 05Demo: replacing anonymous classes with lambdas11 min read

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)

IDOutcomeTask
BEG-035-EX011Convert an anonymous Runnable that prints "hello" into a lambda; explain which interface it implements.
BEG-035-EX022Write 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-EX033Rewrite as method references: s -> s.toUpperCase(), s -> System.out.println(s), () -> new ArrayList<String>().
BEG-035-EX041Write 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 implements Runnable, whose single method is run.
  • 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 (as Supplier<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.

  1. 1A functional interface has:O1
  2. 2(a, b) -> a + b is a:O1
  3. 3A lambda can use a local variable from outside if it is:O1
  4. 4Takes a value, returns boolean:O2
  5. 5Takes nothing, returns a value:O2
  6. 6Takes a value, returns nothing:O2
  7. 7Method name of Function:O2
  8. 8String::length is equal to:O3
  9. 9System.out::println is a reference to:O3
  10. 10If a lambda body needs ten lines, you should:O1

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.

Type to search levels, phases, modules and lessons.