Lambda Expression Basic Example – Java 8

Lambda Expression Basic Example – Java 8

Why Java 8 supports functional programming is a big debate we can avoid for now. Lets try to understand what the features Java 8 has given for supporting functional programming. First thing we hear is Java 8 has provided Lambda Expressions, According to Oracle’s Chief Architect Mark Reinhold, The Single Largest Upgrade Ever. Lets start learning the Lambda expression now.

Lambda’s are anonymous functions which doesn’t need a name, input arguments or return type. If I say like that you might think why and how they are useful ?

Shall we really use them? If you could see a very small and basic example given for Prime Numbers, you would be able to understand the simplicity and ease of use of Lambda’s. Explanation of the code is given below the example.

Note: Please look at the methods [ isPrimeNum(Old School of Coding) and isPrime( New School of Functional Coding)].

package com.ravi.lambda;

import java.util.function.Function;
import java.util.function.Predicate;

public class PrimeNumber {

     public static void main(String[] args) {

          // Old Style
          System.out.println(" 1 is Prime ? -" + isPrimeNum(1));
          System.out.println(" 2 is Prime ? -" + isPrimeNum(2));
          System.out.println(" 3 is Prime ? -" + isPrimeNum(3));
          System.out.println(" 4 is Prime ? -" + isPrimeNum(4));

          // New Style
          System.out.println(" 1 is Prime ? -" + isPrime(1));
          System.out.println(" 2 is Prime ? -" + isPrime(2));
          System.out.println(" 3 is Prime ? -" + isPrime(3));
          System.out.println(" 4 is Prime ? -" + isPrime(4));

     * @param number
     * @return
     static boolean isPrimeNum(Integer number){
          if(number == 1 ){
              return false;
          for(int i=2; i< number; i++){
               if(number % i == 0 ) {
                    return false;
     return true;

     * @param number
     * @return
     static boolean isPrime(int number){
     // Functional or Declarative Way of doing
     return number > 1 && IntStream.range(2, number).noneMatch( index -> number % index == 0 );

Lets look at the isPrimeNum method. Did you notice anything strange ? I guess no, because we know that Java works that way.

Loop over all numbers which are greater than 2 till n-1, as 1 is not a prime number and any number can be divisible by 1, we would like to start with 2. So, we had to handle the case of 1 separately.

Instead of Looping the code we could do it with Functional way –  isPrime demonstrates that code.

The easiest way to identify a Lambda Expression is “->“. A Lambda can take Optional Parameters, and executes the expressions or statements with in the braces.  Let me explain the above example.

First condition we are checking is number greater than 1 and Stream is the new API given by Java 8 [Which can be used in place of Collections… Read More ] which provides sequence of events. I have used “IntStream” which is defined for Integers, which has this Static Method called Range (Runs the loop from 2 till the given number )and noneMatch (returns a Boolean value, whether no elements of this stream match the provided predicate )instance method which take Predicate as input, this is where we have used a Lambda expression to see the number is divisible by the index.

Lambda’s has too many things, I hope this is a very good starting point for you. I would try to give you how to make it generic and re use a Lambda expression in next blog.


Oracle Blog about Lambda Expressions

Lambda Expression FAQ


5 thoughts on “Lambda Expression Basic Example – Java 8

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 )

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