java.util.function


  1. Predicate to check if a string is empty:

Predicate<String> isEmpty = String::isEmpty;
  1. Function to get the length of a string:

Function<String, Integer> length = String::length;
  1. Consumer to print a string:

Consumer<String> print = System.out::println;
  1. Supplier to generate a random number:

Supplier<Integer> randomNumber = () -> (int) (Math.random() * 10);
  1. BiFunction to calculate the area of a rectangle:

BiFunction<Integer, Integer, Integer> area = (width, height) -> width * height;
  1. UnaryOperator to increment a number:

UnaryOperator<Integer> increment = x -> x + 1;
  1. BinaryOperator to add two numbers:

BinaryOperator<Integer> add = (x, y) -> x + y;
  1. Comparator to compare two strings:

Comparator<String> comparator = Comparator.comparing(String::length);
  1. Runnable to create a thread that prints a message:

Runnable runnable = () -> System.out.println("Hello, world!");
  1. BiConsumer to accept two strings and print them:

  1. ToIntFunction to convert a string to an integer:

  1. ToDoubleFunction to convert a string to a double:

  1. ToLongFunction to convert a string to a long:

  1. IntPredicate to check if an integer is even:

  1. DoublePredicate to check if a double is greater than 0:

  1. LongPredicate to check if a long is less than 10:

  1. Consumer to print a formatted string:

  1. BiFunction to create a pair:

  1. BinaryOperator to combine two pairs:

  1. Supplier to generate a list of numbers:

  1. Comparator to compare two lists:

  1. Consumer to print a list of numbers:

  1. BiFunction to calculate the sum of two lists:

  1. UnaryOperator to square a number:

  1. BinaryOperator to multiply two numbers:

  1. Comparator to compare two numbers by their absolute value:

  1. Supplier to generate a random boolean:

  1. Consumer to print a boolean:

  1. BiConsumer to accept two booleans and print them:

  1. ToIntFunction to convert a boolean to an integer:

  1. ToDoubleFunction to convert a boolean to a double:

  1. ToLongFunction to convert a boolean to a long:

  1. IntPredicate to check if an integer is divisible by 3:

  1. DoublePredicate to check if a double is negative:

  1. LongPredicate to check if a long is positive:

  1. Consumer to print an object:

  1. BiConsumer to accept two objects and print them:

  1. Supplier to generate a list of objects:

  1. Comparator to compare two objects by their class name:

  1. Predicate to check if an object is an instance of a particular class:

  1. Function to get the class of an object:

  1. Consumer to print the class of an object:

  1. BiConsumer to accept two objects and print their classes:

  1. Supplier to generate a random object:

  1. Comparator to compare two objects by their hash code:

  1. Predicate to check if an object is null:

  1. Function to get the string representation of an object:

  1. Consumer to print the string representation of an object:

  1. BiConsumer to accept two objects and print their string representations:

  1. Supplier to generate a random list of objects: