Common Built-in Functional Interfaces
Understanding lambda expressions in Java means becoming familiar with several key functional interfaces from the java.util.function package. These interfaces act as the foundation for passing behavior as data, allowing you to write concise, flexible, and reusable code. The most commonly used built-in functional interfaces are Predicate, Function, Consumer, and Supplier.
- Predicate: represents a function that takes one argument and returns a boolean. It's typically used for filtering or matching;
- Function: represents a function that takes one argument and returns a result. It's ideal for transforming or mapping data from one form to another;
- Consumer: represents a function that takes one argument and returns nothing. It's used for performing actions, such as printing or saving, on each element;
- Supplier: represents a function that takes no arguments and returns a result. It's used for providing or generating values on demand.
You will see these interfaces in many Java APIs, especially when working with collections and streams.
Main.java
1234567891011121314151617181920212223242526272829303132333435363738package com.example; import java.util.function.*; import java.util.*; public class Main { public static void main(String[] args) { // Predicate: filter even numbers Predicate<Integer> isEven = n -> n % 2 == 0; List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); List<Integer> evens = new ArrayList<>(); for (Integer n : numbers) { if (isEven.test(n)) { evens.add(n); } } System.out.println("Even numbers: " + evens); // Function: square each number Function<Integer, Integer> square = x -> x * x; List<Integer> squares = new ArrayList<>(); for (Integer n : numbers) { squares.add(square.apply(n)); } System.out.println("Squares: " + squares); // Consumer: print each number Consumer<Integer> printer = x -> System.out.println("Number: " + x); for (Integer n : numbers) { printer.accept(n); } // Supplier: provide a random number Supplier<Double> randomSupplier = () -> Math.random(); System.out.println("Random number: " + randomSupplier.get()); } }
These interfaces are not just theoretical—they are deeply integrated into Java's core APIs. For example, when you use collection operations like removeIf, you supply a Predicate to determine which elements to remove. When you use map operations in streams, you provide a Function to transform each element. Consumer is used in methods like forEach to perform an action for each element, and Supplier is often used for lazy initialization or value generation.
To see a practical example, you can use a Consumer to print every element in a list. This pattern is common when you want to perform an action on each item without returning a result.
Main.java
12345678910111213package com.example; 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> printName = name -> System.out.println("Hello, " + name + "!"); names.forEach(printName); } }
1. Which functional interface is most suitable for transforming an input value to a new output value?
2. Which interface should you use for a lambda that returns true or false based on a condition?
3. Suppose you want to print each element of a list to the console using a lambda expression. Which functional interface should you use?
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 5.56
Common Built-in Functional Interfaces
Veeg om het menu te tonen
Understanding lambda expressions in Java means becoming familiar with several key functional interfaces from the java.util.function package. These interfaces act as the foundation for passing behavior as data, allowing you to write concise, flexible, and reusable code. The most commonly used built-in functional interfaces are Predicate, Function, Consumer, and Supplier.
- Predicate: represents a function that takes one argument and returns a boolean. It's typically used for filtering or matching;
- Function: represents a function that takes one argument and returns a result. It's ideal for transforming or mapping data from one form to another;
- Consumer: represents a function that takes one argument and returns nothing. It's used for performing actions, such as printing or saving, on each element;
- Supplier: represents a function that takes no arguments and returns a result. It's used for providing or generating values on demand.
You will see these interfaces in many Java APIs, especially when working with collections and streams.
Main.java
1234567891011121314151617181920212223242526272829303132333435363738package com.example; import java.util.function.*; import java.util.*; public class Main { public static void main(String[] args) { // Predicate: filter even numbers Predicate<Integer> isEven = n -> n % 2 == 0; List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); List<Integer> evens = new ArrayList<>(); for (Integer n : numbers) { if (isEven.test(n)) { evens.add(n); } } System.out.println("Even numbers: " + evens); // Function: square each number Function<Integer, Integer> square = x -> x * x; List<Integer> squares = new ArrayList<>(); for (Integer n : numbers) { squares.add(square.apply(n)); } System.out.println("Squares: " + squares); // Consumer: print each number Consumer<Integer> printer = x -> System.out.println("Number: " + x); for (Integer n : numbers) { printer.accept(n); } // Supplier: provide a random number Supplier<Double> randomSupplier = () -> Math.random(); System.out.println("Random number: " + randomSupplier.get()); } }
These interfaces are not just theoretical—they are deeply integrated into Java's core APIs. For example, when you use collection operations like removeIf, you supply a Predicate to determine which elements to remove. When you use map operations in streams, you provide a Function to transform each element. Consumer is used in methods like forEach to perform an action for each element, and Supplier is often used for lazy initialization or value generation.
To see a practical example, you can use a Consumer to print every element in a list. This pattern is common when you want to perform an action on each item without returning a result.
Main.java
12345678910111213package com.example; 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> printName = name -> System.out.println("Hello, " + name + "!"); names.forEach(printName); } }
1. Which functional interface is most suitable for transforming an input value to a new output value?
2. Which interface should you use for a lambda that returns true or false based on a condition?
3. Suppose you want to print each element of a list to the console using a lambda expression. Which functional interface should you use?
Bedankt voor je feedback!