Lamdas and Streams
These exercises are designed to gradually introduce students to lambdas in Java, starting from the basics and progressing to more practical use cases like the Streams API and functional interfaces.
Rewrite the following code using a lambda expression:
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("Hello, world!");
}
};
runnable.run();
Goal: Replace the anonymous class with a lambda expression.
Hint: Use the Runnable
functional interface.
Write a lambda expression that takes a string as input and prints it in uppercase. Use this functional interface:
@FunctionalInterface
interface Printer {
void print(String message);
}
public class Main {
public static void main(String[] args) {
Printer printer = ...; // Write the lambda here
printer.print("hello");
}
}
Goal: Replace the ...
with a lambda to convert and print the input in uppercase.
Use the Consumer<T>
functional interface to create a lambda that prints each element in a list:
import java.util.function.Consumer;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
Consumer<String> printer = ...; // Write the lambda here
names.forEach(printer);
}
}
Goal: Replace ...
with a lambda that prints each name.
Use the Function<T, R>
functional interface to create a lambda that doubles an integer:
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
Function<Integer, Integer> doubler = ...; // Write the lambda here
System.out.println(doubler.apply(5)); // Should print 10
}
}
Goal: Replace ...
with a lambda that doubles the input.
Use the Predicate<T>
functional interface to filter a list of numbers:
import java.util.function.Predicate;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
Predicate<Integer> isEven = ...; // Write the lambda here
numbers.stream()
.filter(isEven)
.forEach(System.out::println); // Should print 2, 4, 6
}
}
Goal: Replace ...
with a lambda that checks if a number is even.
Sort a list of strings by their lengths using a lambda expression:
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> words = Arrays.asList("apple", "banana", "kiwi", "cherry");
// Write the lambda inside the sort method
words.sort(...);
System.out.println(words); // Should print [kiwi, apple, banana, cherry]
}
}
Goal: Replace ...
with a lambda that sorts the words by length.
Hints for sorting with lambdas
Use a lambda expression with the map
method to convert a list of integers to their squares:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> squares = numbers.stream()
.map(...) // Write the lambda here
.collect(Collectors.toList());
System.out.println(squares); // Should print [1, 4, 9, 16, 25]
}
}
Goal: Replace ...
with a lambda that calculates the square of each number.
Filter all even numbers from a list and double them using lambdas:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
List<Integer> result = numbers.stream()
.filter(...) // Filter even numbers
.map(...) // Double the numbers
.collect(Collectors.toList());
System.out.println(result); // Should print [4, 8, 12]
}
}
Goal: Replace ...
in filter
and map
with appropriate lambdas.
Create a custom functional interface to calculate the area of a rectangle:
@FunctionalInterface
interface AreaCalculator {
int calculate(int length, int width);
}
public class Main {
public static void main(String[] args) {
AreaCalculator calculator = ...; // Write the lambda here
System.out.println(calculator.calculate(5, 3)); // Should print 15
}
}
Goal: Replace ...
with a lambda to compute the area of a rectangle.
Chain two functions using andThen
to first double a number and then add 5:
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
Function<Integer, Integer> doubler = ...; // Lambda to double a number
Function<Integer, Integer> addFive = ...; // Lambda to add 5
Function<Integer, Integer> combined = doubler.andThen(addFive);
System.out.println(combined.apply(10)); // Should print 25
}
}
Goal: Write lambdas for doubler
and addFive
, then combine them using andThen
.
Use a parallel stream to calculate the sum of squares of a list of integers:
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sumOfSquares = numbers.parallelStream()
.mapToInt(...) // Write the lambda here
.sum();
System.out.println(sumOfSquares); // Should print 55
}
}
Goal: Replace ...
with a lambda that squares each number.