Is Comparator a function interface, but it has two abstract methods?

Review the Comparator class; it has two abstract methods, why it can be a function interface? Comparator.java package java.util; @FunctionalInterface public interface Comparator<T> { // abstract method int compare(T o1, T o2); // abstract method boolean equals(Object obj); // few default and static methods } Definition of function interface Conceptually, a functional interface has exactly …

Read more

Java 8 method references, double colon (::) operator

In Java 8, the double colon (::) operator is called method references. Refer to the following examples: Anonymous class to print a list. List<String> list = Arrays.asList("node", "java", "python", "ruby"); list.forEach(new Consumer<String>() { // anonymous class @Override public void accept(String str) { System.out.println(str); } }); Anonymous class -> Lambda expressions. List<String> list = Arrays.asList("node", "java", …

Read more

Java 8 Supplier Examples

In Java 8, Supplier is a functional interface; it takes no arguments and returns a result. Supplier.java @FunctionalInterface public interface Supplier<T> { T get(); } 1. Supplier 1.1 This example uses Supplier to return a current date-time. Java8Supplier1.java package com.mkyong; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.function.Supplier; public class Java8Supplier1 { private static final DateTimeFormatter dtf …

Read more

Java 8 UnaryOperator Examples

In Java 8, UnaryOperator is a functional interface and it extends Function. The UnaryOperator takes one argument, and returns a result of the same type of its arguments. UnaryOperator.java @FunctionalInterface public interface UnaryOperator<T> extends Function<T, T> { } The Function takes one argument of any type and returns a result of any type. Function.java @FunctionalInterface …

Read more

Java 8 BinaryOperator Examples

In Java 8, BinaryOperator is a functional interface and it extends BiFunction. The BinaryOperator takes two arguments of the same type and returns a result of the same type of its arguments. BinaryOperator.java @FunctionalInterface public interface BinaryOperator<T> extends BiFunction<T,T,T> { } The BiFunction takes two arguments of any type, and returns a result of any …

Read more

Java 8 BiConsumer Examples

In Java 8, BiConsumer is a functional interface; it takes two arguments and returns nothing. @FunctionalInterface public interface BiConsumer<T, U> { void accept(T t, U u); } Further Reading – Java 8 Consumer Examples 1. BiConsumer JavaBiConsumer1.java package com.mkyong.java8; import java.util.function.Consumer; public class JavaBiConsumer1 { public static void main(String[] args) { BiConsumer<Integer, Integer> addTwo = …

Read more

Java 8 Consumer Examples

In Java 8, Consumer is a functional interface; it takes an argument and returns nothing. @FunctionalInterface public interface Consumer<T> { void accept(T t); } 1. Consumer Java8Consumer1.java package com.mkyong.java8; import java.util.function.Consumer; public class Java8Consumer1 { public static void main(String[] args) { Consumer<String> print = x -> System.out.println(x); print.accept("java"); // java } } Output java 2. …

Read more

Java 8 BiPredicate Examples

In Java 8, BiPredicate is a functional interface, which accepts two arguments and returns a boolean, basically this BiPredicate is same with the Predicate, instead, it takes 2 arguments for the test. @FunctionalInterface public interface BiPredicate<T, U> { boolean test(T t, U u); } Further Reading Java 8 Predicate Examples 1. BiPredicate Hello World. If …

Read more

Java 8 Predicate Examples

In Java 8, Predicate is a functional interface, which accepts an argument and returns a boolean. Usually, it used to apply in a filter for a collection of objects. @FunctionalInterface public interface Predicate<T> { boolean test(T t); } Further Reading Java 8 BiPredicate Examples 1. Predicate in filter() filter() accepts predicate as argument. Java8Predicate.java package …

Read more