Mastering Method References: A Deep Dive into Java 8

In Java 8, a method reference is a shorthand syntax for a lambda expression that just calls an existing method. A method reference can be thought of as a type of function pointer in Java, referring to a method of an object or a static method.

There are four types of method references in Java 8:

  • Reference to a static method
  • Reference to an instance method of an arbitrary object of a particular type
  • Reference to an instance method of a particular object
  • Reference to a constructor

Method references make it easier to pass methods as arguments to higher-order functions and can make code more readable and concise.

Reference to a static method:

In Java 8, a method reference to a static method is a shorthand syntax for a lambda expression that just calls the static method. The syntax for a method reference to a static method is ClassName::methodName.

For example, consider the following code that sorts a list of strings based on the length of the string, using a lambda expression,

List<String> strings = Arrays.asList("abc", "defg", "h");
strings.sort((s1, s2) -> Integer.compare(s1.length(), s2.length()));

The same sorting can be achieved using a method reference to a static method as follows:

strings.sort(Comparator.comparingInt(String::length));

In this example, the comparingInt method takes a ToIntFunction as an argument and returns a Comparator that compares two elements based on the result of applying the function to each element. The String::length method reference is a shorthand for a lambda expression that calls the length method on a String object, so it can be used as the argument to comparingInt.

Reference to an instance method of an arbitrary object of a particular type:

n Java 8, a method reference to an instance method of an arbitrary object of a particular type is a shorthand syntax for a lambda expression that just calls the instance method on the given type. The syntax for a method reference to an instance method of an arbitrary object of a particular type is TypeName::methodName.

For example, consider the following code that sorts a list of Person objects based on their age, using a lambda expression:

List<Person> people = Arrays.asList(new Person("John", 30), new Person("Jane", 25), new Person("Jim", 35));
people.sort((p1, p2) -> Integer.compare(p1.getAge(), p2.getAge()));

The same sorting can be achieved using a method reference to an instance method of an arbitrary object of a particular type as follows:

people.sort(Comparator.comparingInt(Person::getAge));

The Person::getAge method reference is a shorthand for a lambda expression that calls the getAge method on a Person object, so it can be used as the argument to comparingInt.

Reference to an instance method of a particular object:

The syntax for a method reference to an instance method of a particular object is objectReference::methodName.

For example, consider the following code that sorts a list of Person objects based on their name length, using a lambda expression:

List<Person> people = Arrays.asList(new Person("John", 30), new Person("Jane", 25), new Person("Jim", 35));
StringLengthComparator comparator = new StringLengthComparator();
people.sort((p1, p2) -> comparator.compareByNameLength(p1, p2));

The same sorting can be achieved using a method reference to an instance method of a particular object as follows:

StringLengthComparator comparator = new StringLengthComparator();
people.sort(comparator::compareByNameLength);

In this example, the compareByNameLength method takes two Person objects as arguments and returns an integer that represents the difference between the lengths of the names of the two objects. The comparator::compareByNameLength method reference is a shorthand for a lambda expression that calls the compareByNameLength method on the comparator object, so it can be used as the argument to sort.

Reference to a constructor

Method reference to a constructor is a shorthand syntax for a lambda expression that creates an object of a particular type using the constructor. The syntax for a method reference to a constructor is ClassName::new.

For example, consider the following code that creates a list of Person objects using a lambda expression:

List<Person> people = Arrays.asList("John", "Jane", "Jim").stream()
        .map(name -> new Person(name, 0))
        .collect(Collectors.toList());

The same code can be written using a method reference to a constructor as follows:

List<Person> people = Arrays.asList("John", "Jane", "Jim").stream()
        .map(Person::new)
        .collect(Collectors.toList());

In this example, the Person::new method reference is a shorthand for a lambda expression that calls the Person constructor with a single String argument, so it can be used as the argument to map. This allows us to create Person objects by calling the constructor with the values from the stream.

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *