In the Java 8 method reference example, i show you how to use a method from class or object using class::methodName type syntax. Method reference is a new feature called in Java 8 and is used to refer method of functional interface. In this tutorial, we are explaining method reference concept in detail.
Types of Method References
There are four types of method references in Java 8:
- Reference to a static method.
- Reference to an instance method of a particular object.
- Reference to an instance method of an arbitrary object of a particular type.
- Reference to a constructor.
1. Reference to static method: Used to refer static methods from a class
Let’s see example to use ClassName.isOdd() which is static method. Here, ClassName::isOdd equivalent to ClassName.isOdd(x).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class ReferenceToStaticMethod { public static void main(String args[]) { List<Integer> integers = Arrays.asList(6,1,12,433,5); //using lambda expression Stream<Integer> maxLd = integers.stream().filter(x -> ReferenceToStaticMethod.isOdd(x)); maxLd.forEach(value -> System.out.println(value)); //using method reference Stream<Integer> max = integers.stream().filter(ReferenceToStaticMethod::isOdd); max.forEach(value -> System.out.println(value)); } public static boolean isOdd(Integer n) { return n % 2 != 0; } } |
It should produce the following output
2. Reference to instance method from instance: Refer to an instance method using a reference to the supplied object. We can use System.out::println to print the value. Since System.out is an instance of type PrintStream. Here, System.out::println equivalent to System.out.println(x)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class ReferenceToInstanceMethod { public static void main(String args[]) { List<Integer> integers = Arrays.asList(6,1,12,433,5); //using lambda expression Stream<Integer> maxLd = integers.stream().filter(x -> ReferenceToStaticMethod.isOdd(x)); maxLd.forEach(x -> System.out.println(x)); //using method reference Stream<Integer> max = integers.stream().filter(ReferenceToInstanceMethod::isOdd); max.forEach(System.out::println); } public static boolean isOdd(Integer n) { return n % 2 != 0; } } |
It should produce the following output
3. Reference to instance method from class type: using the syntax Class::instanceMethodName, in this example we use String::compareTo equivalent to x.compareTo(y).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class ReferenceToInstanceMethodClassType { public static void main(String args[]) { List<String> strings = Arrays .asList("Reference", "To", "Instance", "Method", "From","Class","Type"); //using lambda expression List<String> sortedLd = strings .stream() .sorted((x, y) -> x.compareTo(y)) .collect(Collectors.toList()); sortedLd.forEach(x -> System.out.println(x)); //using method reference List<String> sorted = strings .stream() .sorted(String::compareTo) .collect(Collectors.toList()); sorted.forEach(System.out::println); } } |
It should produce the following output
4. Reference to constructor: we use ArrayList::new equivalent to new ArrayList(), see detail in example below.
1 2 3 4 5 6 7 8 9 10 11 |
public class ReferenceToConstructor { public static void main(String args[]) { List<Integer> integers = IntStream.range(10000, 10006).boxed().collect(Collectors.toCollection(ArrayList::new)); integers.stream().forEach(System.out::println); } } |
It should produce the following output
That’s all about Java 8 method reference example.
Reference
Method References
Download complete source code, click link below
Java-Method-Reference.zip (257 downloads)