Java 8 New Features: Method Reference

Coming with lambda feature in Java 8 is the method reference feature. It allows programmers to use methods as variables, and parameters to other methods. It works for class constructors too, where the method name is unanimously “new.’

There are four types of method references per Oracle documentation. Syntax wise, they all look similar with double colons as shown in the following sample code.

Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.

As you’ll see, the sample code sort an array of Integer objects with two different ways. I picked Integer as the element for sorting simply because it’s part of standard library and everyone knows about it. More importantly, I don’t need to write another Java POJO class for the sample. :)

/* Copyright by Steve Jin. All Rights Reserved. */
 
package org.doublecloud.jave8demo.defaultmethod;
 
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
 
public class MethodRef
{
   public static void main(String[] args)
  {
    Integer[] a = new Integer[] { 100_000_000, -1, 1, 2, 3};
 
    Comparator<? super Integer> f = CompareUtil::compareByValue;
    Arrays.sort(a, f);
    for(Integer i : a)
    {
      System.out.println(i);
    }
 
    System.out.println("=======");
 
    List<Integer> al = Arrays.asList(a);
    al.sort(CompareUtil::compareByValue);
    al.forEach(e -> System.out.println(e));
  }
}
 
class CompareUtil
{
  public static int compareByValue(Integer a, Integer b)
  {
    return a.compareTo(b);
  }
}

As you can see, the compareByValue() method is assigned to a variable typed Comparator (this is really not needed but just to show it can be treated as a variable). The variable can then be used as parameter to the sort method.

In the second way of sorting, the array is converted as a List and the sort() method defined on the List then uses the method reference directly as CompareUtil::compareByValue.

Because of this feature and lambda, the Java 8 collector library has quite some changes, I think the biggest after the generics. Although it takes a bit time to get used to the new way, the benefit can be huge with cleaner code and lots of built-in functions to reference. If you are interested in finding out, you can take a look at the java.util.stream.Collectors which has many methods defined and meant for being referenced elsewhere.

In the next article, I will introduce the stream API for parallel processing large amount of data. Stay tuned.

This entry was posted in Software Development and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted April 19, 2014 at 8:33 am | Permalink

    That is a great. I am converted to be a C# programmer for a few years.
    Java is improved a lot, but this method reference is in C# for a while.
    That is good to Java learn from C#, C# learn from Java.

  2. Posted April 19, 2014 at 11:25 am | Permalink

    That is true. The companies behind also hire designers from each other. :)

    Steve

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.