In my previous articles, we have looked at Lambda Expression, Streams API and Default Methods. In this latest article in the series, we will be looking at Method References.
What is Method References?
It is a feature which is related to Lambda Expression. It allows us to reference constructors or methods without executing them. Method references and Lambda are similar in that they both require a target type that consist of a compatible functional interface.
Types of Method Reference
There are four types of method reference, the table below summarizes this.
![]() |
1 . Reference to a Static Method
- public class ReferenceToStaticMethodExample {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16);
- List primeNumbers = ReferenceToStaticMethodExample.findPrimeNumbers(numbers,
- (number) -> ReferenceToStaticMethodExample.isPrime((int) number));
- System.out.println("Prime Numbers are " + primeNumbers);
- }
- public static boolean isPrime(int number) {
- if (number == 1) {
- return false;
- }
- for (int i = 2; i < number; i++) {
- if (number % i == 0) {
- return false;
- } }
- return true;
- }
- public static List findPrimeNumbers(List list, Predicate predicate) {
- List sortedNumbers = new ArrayList();
- list.stream().filter((i) -> (predicate.test(i))).forEach((i) -> {
- sortedNumbers.add(i);
- });
- return sortedNumbers;
- }
- }
ContainingClass::staticMethodName
![]() |
- public class ReferenceToConstructor {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- List numbers = Arrays.asList(4,9,16,25,36);
- List squaredNumbers = ReferenceToConstructor.findSquareRoot(numbers,Double::new);
- System.out.println("Square root of numbers = "+squaredNumbers);
- }
- private static List findSquareRoot(List list, Function<double,double -> f){
- List result = new ArrayList();
- list.forEach(x -> result.add(f.apply(Math.sqrt(x))));
- return result;
- }
- }
3. Reference To an Instance Method Of An Arbitrary Object Of A Particular Type
- public class ReferenceToInstanceMethodAOPT {
- /**
- * @param args the command line arguments
- */
- private static class Person {
- private final String name;
- private final int age;
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- }
- public static void main(String[] args) {
- // TODO code application logic here
- List persons = new ArrayList();
- persons.add(new Person("Albert", 80));
- persons.add(new Person("Ben", 15));
- persons.add(new Person("Charlote", 20));
- persons.add(new Person("Dean", 6));
- persons.add(new Person("Elaine", 17));
- List allAges = ReferenceToInstanceMethodAOPT.listAllAges(persons, Person::getAge);
- System.out.println("Printing out all ages \n"+allAges);
- }
- private static List listAllAges(List person, Function < person, integer="" -> f){
- List result = new ArrayList();
- person.forEach(x -> result.add(f.apply(x)));
- return result;
- }
- }
![]() |
- public class ReferenceToInstanceMethodOAPO {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- List names = new ArrayList();
- names.add("David");
- names.add("Richard");
- names.add("Samuel");
- names.add("Rose");
- names.add("John");
- ReferenceToInstanceMethodOAPO.printNames(names,System.out::println);
- }
- private static void printNames(List list, Consumer c ){
- list.forEach(x -> c.accept(x));
- }
- }
![]() |
- You can use replace Lambda Expressions with Method References where Lamdba is invoking already defined methods.
- You can’t pass arguments to methods Reference
- To use Lambda and Method Reference, make sure you have Java 8 installed. They do not work on Java 7 and earlier versions.
Written by Ernest Duodu
If you found this post interesting, follow and support us.
Suggest for you:
The Complete Java Developer Course
Java Programming For Beginners
Complete Java For Selenium WebDriver And Test Automation
The Complete Android & Java Course - Build 21 Android Apps
Java for Android Development - Getting Started (Khmer)
No comments:
Post a Comment