Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 13:16:53 Viewed : 230
In Java 8, the java.util.function
package was introduced to provide a set of functional interfaces to represent commonly used functional interfaces in the context of the new features like lambda expressions and the Stream API. These interfaces can be used as types for lambda expressions and method references. Here are some key interfaces in the java.util.function
package:
Consumer<T>:
void accept(T t)
.BiConsumer<T, U>:
void accept(T t, U u)
.Supplier<T>:
T get()
.Function<T, R>:
R apply(T t)
.BiFunction<T, U, R>:
R apply(T t, U u)
.UnaryOperator<T>:
T apply(T t)
.BinaryOperator<T>:
T apply(T t1, T t2)
.Predicate<T>:
boolean test(T t)
.BiPredicate<T, U>:
boolean test(T t, U u)
.Consumer<T>:
void accept(T t)
.BiConsumer<T, U>:
void accept(T t, U u)
.These functional interfaces are designed to be used in functional programming scenarios, making it easier to work with lambda expressions and streams. They provide a standard set of interfaces that cover common use cases for functions, consumers, suppliers, and predicates.
Below are examples of some key functional interfaces from the java.util.function
package in Java 8 along with sample code and output:
Consumer<T>:
javaimport java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("John", "Jane", "Bob");
// Using Consumer to print each element
Consumer<String> printName = name -> System.out.println(name);
names.forEach(printName);
}
}
Output:
John Jane Bob
Function<T, R>:
javaimport java.util.function.Function;
public class FunctionExample {
public static void main(String[] args) {
// Using Function to convert String to Integer
Function<String, Integer> stringLength = s -> s.length();
int length = stringLength.apply("Java 8");
System.out.println("Length of Java 8 : " + length);
}
}
Output:
arduino Length of Java 8 : 6
Predicate<T>:
javaimport java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class PredicateExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Using Predicate to filter even numbers
Predicate<Integer> isEven = number -> number % 2 == 0;
numbers.stream().filter(isEven).forEach(System.out::println);
}
}
Output:
2 4
Supplier<T>:
javaimport java.util.function.Supplier;
public class SupplierExample {
public static void main(String[] args) {
// Using Supplier to generate a random number
Supplier<Double> randomSupplier = Math::random;
System.out.println("Random Number: " + randomSupplier.get());
}
}
Output:
mathematicaRandom Number: 0.123456789
These examples demonstrate the use of functional interfaces from the java.util.function
package to perform common functional programming tasks. You can see how these interfaces are used with lambda expressions to provide concise and expressive code.