Mapping Maps With Method References

I just learned about “method reference” notation in Java 8 thanks to Intellij IDEA code hints.

Consider the following code:

public class MapMap
   Map<String, String> map;
   public static void map(Object key, Object value)
      System.out.println(key + ":" + value);
   public void mapmap()
   { //...see implementations below }

I knew this:

/* loop */ 
for (Map.Entry entry: map.entrySet()) 
  map(entry.getKey(), entry.getValue()); 

could be replaced with this:

/* lambda expression */ 
map.forEach((key, value) -> map(key, value);

but I did not know about this:

/* method reference */ 

See the full gist here


See here for the official documentation on method references:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s