Functional 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
1234567891011121314151617package 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
12345678910111213141516171819package 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?
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Can you give examples of how to use these functional interfaces with lambda expressions?
What is the purpose of the @FunctionalInterface annotation in practice?
Are there any rules or limitations when creating custom functional interfaces?
Awesome!
Completion rate improved to 5.56
Functional Interfaces Explained
Deslize para mostrar o 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
1234567891011121314151617package 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
12345678910111213141516171819package 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?
Obrigado pelo seu feedback!