Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Functional Interfaces Explained | Getting Started with Lambda Expressions
Quizzes & Challenges
Quizzes
Challenges
/
Lambda Expressions in Java

bookFunctional Interfaces Explained

To work effectively with lambda expressions in Java, you must understand the concept of functional interfaces. A functional interface is an interface that contains exactly one abstract method. This single abstract method defines the contract for the lambda expression, allowing you to provide behavior in a concise way.

Java provides the @FunctionalInterface annotation to indicate that an interface is intended to be a functional interface. This annotation is not required, but it helps the compiler and other developers by enforcing the rule that only one abstract method is present. If you mistakenly add a second abstract method, the compiler will generate an error if the annotation is present.

Main.java

Main.java

copy
1234567891011121314151617
package com.example; @FunctionalInterface interface StringProcessor { String process(String input); } public class Main { public static void main(String[] args) { StringProcessor toUpperCase = (s) -> s.toUpperCase(); StringProcessor addExclamation = (s) -> s + "!"; System.out.println(toUpperCase.process("hello")); // Output: HELLO System.out.println(addExclamation.process("hello")); // Output: hello! } }

Functional interfaces are the foundation for lambda expressions in Java. When you write a lambda expression, you are essentially providing an implementation for the single abstract method of a functional interface. This is why lambda expressions can only be assigned to variables of functional interface types.

Java's standard library provides many built-in functional interfaces in the java.util.function package. Some common examples include:

  • Predicate<T>: represents a boolean-valued function of one argument;
  • Function<T, R>: represents a function that accepts one argument and produces a result;
  • Consumer<T>: represents an operation that accepts a single input argument and returns no result;
  • Supplier<T>: represents a supplier of results.

These interfaces make it easy to use lambda expressions for common programming tasks, such as filtering, mapping, and consuming data.

Main.java

Main.java

copy
12345678910111213141516171819
package com.example; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<String> words = List.of("apple", "banana", "pear", "kiwi"); Predicate<String> isShortWord = word -> word.length() <= 4; List<String> shortWords = words.stream() .filter(isShortWord) .collect(Collectors.toList()); System.out.println(shortWords); // Output: [pear, kiwi] } }

1. What makes an interface a functional interface?

2. Which annotation is used to indicate a functional interface?

3. Given the following interface, can it be used as a target for a lambda expression?

question mark

What makes an interface a functional interface?

Select the correct answer

question mark

Which annotation is used to indicate a functional interface?

Select the correct answer

question-icon

Given the following interface, can it be used as a target for a lambda expression?

Yes, because it has a single abstract method;No, because it has no annotation;No, because it has more than one method;No, because it is not public.
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookFunctional Interfaces Explained

Scorri per mostrare il menu

To work effectively with lambda expressions in Java, you must understand the concept of functional interfaces. A functional interface is an interface that contains exactly one abstract method. This single abstract method defines the contract for the lambda expression, allowing you to provide behavior in a concise way.

Java provides the @FunctionalInterface annotation to indicate that an interface is intended to be a functional interface. This annotation is not required, but it helps the compiler and other developers by enforcing the rule that only one abstract method is present. If you mistakenly add a second abstract method, the compiler will generate an error if the annotation is present.

Main.java

Main.java

copy
1234567891011121314151617
package com.example; @FunctionalInterface interface StringProcessor { String process(String input); } public class Main { public static void main(String[] args) { StringProcessor toUpperCase = (s) -> s.toUpperCase(); StringProcessor addExclamation = (s) -> s + "!"; System.out.println(toUpperCase.process("hello")); // Output: HELLO System.out.println(addExclamation.process("hello")); // Output: hello! } }

Functional interfaces are the foundation for lambda expressions in Java. When you write a lambda expression, you are essentially providing an implementation for the single abstract method of a functional interface. This is why lambda expressions can only be assigned to variables of functional interface types.

Java's standard library provides many built-in functional interfaces in the java.util.function package. Some common examples include:

  • Predicate<T>: represents a boolean-valued function of one argument;
  • Function<T, R>: represents a function that accepts one argument and produces a result;
  • Consumer<T>: represents an operation that accepts a single input argument and returns no result;
  • Supplier<T>: represents a supplier of results.

These interfaces make it easy to use lambda expressions for common programming tasks, such as filtering, mapping, and consuming data.

Main.java

Main.java

copy
12345678910111213141516171819
package com.example; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<String> words = List.of("apple", "banana", "pear", "kiwi"); Predicate<String> isShortWord = word -> word.length() <= 4; List<String> shortWords = words.stream() .filter(isShortWord) .collect(Collectors.toList()); System.out.println(shortWords); // Output: [pear, kiwi] } }

1. What makes an interface a functional interface?

2. Which annotation is used to indicate a functional interface?

3. Given the following interface, can it be used as a target for a lambda expression?

question mark

What makes an interface a functional interface?

Select the correct answer

question mark

Which annotation is used to indicate a functional interface?

Select the correct answer

question-icon

Given the following interface, can it be used as a target for a lambda expression?

Yes, because it has a single abstract method;No, because it has no annotation;No, because it has more than one method;No, because it is not public.
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2
some-alt