Java 8's — Consumer, Predicate, Supplier, and Function.

Functional programming is a paradigm that allows programming using expressions i.e. declaring functions, passing functions as arguments, and using functions as statements. In Java 8’s several functional interfaces are introduced by Java.

Consumer

A Consumer is an in-build functional interface in the java.util.function package. we use consumers when we need to consume objects, the consumer takes an input value and returns nothing. The consumer interface has two methods.

void accept(T value);
default Consumer<T> andThen(Consumer<? super T> after);

Here, we printing the cities by creating a consumer. passing city in consumer as argument and printing.

@Test
public void printCities() {

List<String> cities = new ArrayList<>();
cities.add("Delhi");
cities.add("Mumbai");
cities.add("Goa");
cities.add("Pune");

Consumer<String> printConsumer= city-> System.out.println(city);
cities.forEach(printConsumer);
}

Predicate

A Predicate is a functional interface, which accepts an argument and returns a boolean. Usually, it is used to apply in a filter for a collection of objects.

boolean test(T value);
default Predicate<T> and(Predicate<? super T> other);
default Predicate<T> negate();
default Predicate<T> or(Predicate<? super T> other);
static <T> Predicate<T> isEqual(Object targetRef);
static <T> Predicate<T> not(Predicate<? super T> target);

Here, we filter cities according to city name by a predicate and print it on the console.

@Test
public void filterCities() {

List<String> cities = new ArrayList<>();
cities.add("Delhi");
cities.add("Mumbai");
cities.add("Goa");
cities.add("Pune");

Predicate<String> filterCity = city -> city.equals("Mumbai");
cities.stream().filter(filterCity).forEach(System.out::println);
}

Function

A Function is another in-build functional interface in java.util.function package, the function takes an input value and returns a value. The function interface has four methods, mostly function used in map feature of stream APIs.

R apply(T var1);
default <V> Function<V, R> compose(Function<V, T> before);
default <V> Function<T, V> andThen(Function<R, V> after);
static <T> Function<T, T> identity();

We take an example, here we passing city’s as argument and returning the first character of cities.

@Test
public void mapCities() {

List<String> cities = new ArrayList<>();
cities.add("Delhi");
cities.add("Mumbai");
cities.add("Goa");
cities.add("Pune");

Function<String, Character> getFirstCharFunction = city -> {
return city.charAt(0);
};
cities.stream().map(getFirstCharFunction)
.forEach(System.out::println);
}

Supplier

The Supplier Interface is a part of the java.util.function package. It represents a function that does not take in any argument but produces a value of type T. It contains only one method.

T get();

We created a city’s supplier for producing cities list and printing it.

@Test
public void supplyCities() {

Supplier<String[]> citySupplier = () -> {
return new String[]{"Mumbai", "Delhi", "Goa", "Pune"};
};
Arrays.asList(citySupplier.get()).forEach(System.out::println);
}

There are also few specialized versions of the Consumer

      BiConsumer<T, U>,BiFunction<T, U, R>, BiPredicate<T, U>      
BooleanSupplier
DoubleConsumer, DoubleFunction<R>, DoublePredicate,
DoubleSupplier,DoubleToIntFunction,DoubleToLongFunction

IntConsumer, IntFunction<R>, IntPredicate, IntSupplier,
IntToDoubleFunction, IntToLongFunction
LongConsumer ,LongFunction<R>, LongPredicate, LongSupplier
LongToDoubleFunction, LongToIntFunction

ObjDoubleConsumer<T>, ObjIntConsumer<T>, ObjLongConsumer<T>

ToDoubleBiFunction<T, U>, ToDoubleFunction<T>,
ToIntBiFunction<T, U>, ToIntFunction<T>,
ToLongBiFunction<T, U>, ToLongFunction<T>

Conclusion -

I tried to explain in brief with short samples. I hope this article helped you to learn Java 8 functional interface programming. Thank you !!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store