Java 8 – Re using Lambda Expressions

Java 8 – Re using Lambda Expressions


In my previous Java 8 Lambda expression blog, I tried to explain what is a Lambda expression in general, how to create a Lambda expression in Java 8 etc. Here we are going to look at the re usability perspective of the Lambda expression. It is very much required to re use the code or apply the DRY design principle when we are programming.

Let’s take some example to start with, Here I am trying to create a List of countries for each region, and trying to find the number of countries start with letter say “B” or “N”. Perhaps, to achieve the same result in the conventional way of Java we should run a loop by Iterating the list and putting a if condition and lot of unnecessary code.

Here is how the traditional code looks like


public class OldWay {
     final static List<String> asia =
     Arrays.asList("India", "Nepal", "China", "Singapore", "Srilanka", "Bhutan", "Bangladesh");

     final static List<String> europe =
     Arrays.asList("Netherlands", "Denmark", "Sweden", "Norway", "Ireland", "Britain", "Spain");

public static void main(String[] args) {
     int count = 0;
     for(String ctry : asia){
     if(ctry.substring(0,1).equals("N")){ count ++; }
}
     System.out.println("Countries name starting with N :" + count);
     }
}

So, we have a much better way to achieve the same thing with lambda expressions. It is easy and simple. We need to tune our Java brains a bit to think in functional way. For me it took long time to tune myself 🙂


package com.ravi.lambda.advanced;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class ExampleCollections {

     public static void main(String[] args) {
          final List<String> asia =
          Arrays.asList("India", "Nepal", "China", "Singapore", "Srilanka", "Bhutan", "Bangladesh");

          final List<String> europe =
          Arrays.asList("Netherlands", "Denmark", "Sweden", "Norway", "Ireland", "Britain", "Spain");

         // re using the Lambda Expression ...
         final Predicate<String> startsWithB = name -> name.startsWith("B");

         final long countStartswithBinAsia = asia.stream().filter(startsWithB).count();

         System.out.println(" Asian Countries starts with B :" + countStartswithBinAsia);

         final long countStartswithBinEurope = europe.stream().filter(startsWithB).count();

         System.out.println(" European Countries starts with B :" + countStartswithBinEurope);

       }

}

Output:


Asian Countries starts with B :2
European Countries starts with B :1

Now in the above example we have seen a Predicate which is getting re used as a method in Java. But there is a problem here, as we are not able to reuse this predicate for any other character than B. Below we would look at the Predicate as a full fledged method and could be re usable in any other class with a parameter to it. This is the best way to re use a predicate.


package com.ravi.lambda.advanced;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class ExampleCollections {

// re using the Lambda Expression ... with whatever character you want !!!
public static Predicate<String> startsWithCharacter(final String character) {
return name -> name.startsWith(character);
}

public static void main(String[] args) {
     final List<String> asia =
     Arrays.asList("India", "Nepal", "China", "Singapore", "Srilanka", "Bhutan", "Bangladesh");

     final List<String> europe =
     Arrays.asList("Netherlands", "Denmark", "Sweden", "Norway", "Ireland", "Britain", "Spain");

    final long countStartswithBinAsia = asia.stream().filter(startsWithCharacter("N")).count();

    System.out.println(" Asian Countries starts with N :" + countStartswithBinAsia);

     final long countStartswithBinEurope = europe.stream().filter(startsWithCharacter("N")).count();

     System.out.println(" European Countries starts with N :" + countStartswithBinEurope);

     }

}

Output:


Asian Countries starts with N :1
European Countries starts with N :2

Hope this was useful to you and in next article, I would like to discuss the FunctionalInterface in Java 8.
Thanks and Happy Reading…

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s