Java8 - The java.util.function package

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:

  1. Consumer<T>:

    • Represents an operation that accepts a single input argument and returns no result.
    • Method: void accept(T t).
  2. BiConsumer<T, U>:

    • Represents an operation that accepts two input arguments and returns no result.
    • Method: void accept(T t, U u).
  3. Supplier<T>:

    • Represents a supplier of results.
    • Method: T get().
  4. Function<T, R>:

    • Represents a function that takes one argument and produces a result.
    • Method: R apply(T t).
  5. BiFunction<T, U, R>:

    • Represents a function that takes two arguments and produces a result.
    • Method: R apply(T t, U u).
  6. UnaryOperator<T>:

    • Represents an operation on a single operand that produces a result of the same type as its operand.
    • Method: T apply(T t).
  7. BinaryOperator<T>:

    • Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
    • Method: T apply(T t1, T t2).
  8. Predicate<T>:

    • Represents a predicate (boolean-valued function) of one argument.
    • Method: boolean test(T t).
  9. BiPredicate<T, U>:

    • Represents a predicate (boolean-valued function) of two arguments.
    • Method: boolean test(T t, U u).
  10. Consumer<T>:

    • Represents an operation that accepts a single input argument and returns no result.
    • Method: void accept(T t).
  11. BiConsumer<T, U>:

    • Represents an operation that accepts two input arguments and returns no result.
    • Method: 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:

  1. Consumer<T>:

    java
    import 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
  2. Function<T, R>:

    java
    import 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
  3. Predicate<T>:

    java
    import 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
  4. Supplier<T>:

    java
    import 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:

    mathematica
    Random 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.

Search
Related Articles

Leave a Comment: