Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Common Built-in Functional Interfaces | Getting Started with Lambda Expressions
Lambda Expressions in Java

bookCommon 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

Main.java

copy
1234567891011121314151617181920212223242526272829303132333435363738
package 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

Main.java

copy
12345678910111213
package 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?

question mark

Which functional interface is most suitable for transforming an input value to a new output value?

Select the correct answer

question mark

Which interface should you use for a lambda that returns true or false based on a condition?

Select the correct answer

question-icon

Suppose you want to print each element of a list to the console using a lambda expression. Which functional interface should you use?

PredicateFunctionSupplier

Click or drag`n`drop items and fill in the blanks

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you show me an example of using a Consumer to print elements in a list?

How do I use these functional interfaces with Java streams?

What are some real-world scenarios where these interfaces are useful?

bookCommon Built-in Functional Interfaces

Swipe um das Menü anzuzeigen

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

Main.java

copy
1234567891011121314151617181920212223242526272829303132333435363738
package 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

Main.java

copy
12345678910111213
package 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?

question mark

Which functional interface is most suitable for transforming an input value to a new output value?

Select the correct answer

question mark

Which interface should you use for a lambda that returns true or false based on a condition?

Select the correct answer

question-icon

Suppose you want to print each element of a list to the console using a lambda expression. Which functional interface should you use?

PredicateFunctionSupplier

Click or drag`n`drop items and fill in the blanks

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5
some-alt