Over a million developers have joined DZone.

Lambda Expression in Java SE 8

· Java Zone

Learn more about the advantages of moving from a monolithic to microservices architecture.  Brought to you in partnership with IBM.


In Object Oriented programming languages, object is the first class citizen but reverse is the case in functional programming languages because functions are first class citizens in which they can be passed around as if they are variables this is seen in Haskell, javascript and other functional languges. Functional languages have other constructs that are not readily available in object oriented languages such as closure, lambda expressions and so on. 

Lambda expression was not part of earlier versions of Java and as such, passing method or function as arguments was normally achieved through the use of anonymous class which was not elegant. But with the introduction of Lambda Expression in Java 8 the language can be made to behave like a functional language, though not with closure anyway. Thus, the introduction of Lambda Expression in Java 8 in a way bridges the gap the language has with functional programming language. 

Lambda Expression is usually referred to as anonymous function, a function without a name. It is a shorthand that lets you declare a method where it is going to be used without specifying the method name, return type, access specifier. It enables you to treat code as data and use function as method arguments. 

Why you should care about Lambda Expression
  1. The introduction of lambda expression in java will make available the benefits of functional language in java.
  2. Lambda expression provides a means to write clear and concise codes with the use of expressions.
  3. Collection filtering, Iteration and Extraction can be greatly improved using Lambda expression.

NB: JDK 8 or a higher version is required 


    (argument) -> (body)

Where there can be zero or more arguments, The arrow token is the java lambda operator and (body) contains one or more line of codes to be executed when the function is called. 


(int a, int b) - > a*b
(Person p) - > { return p.getAge();}
In the first example, the expression's arguments are the integer values a and b which would be used by the expression to return the output of the multiplication operation. 

The second example takes an object person as argument while the expression returns the value from the getAge() method called on the object. 

Prior to Java 8, codes to handle EventListener in swing were normally written using the anonymous class
btnOk.setOnAction( newEventHandler<ActionEvent>() {
public voidhandle(ActionEvent e) {
   		 // codes to handle button event would be written here
With lambda expression, the EventListener code can be made to be elegant with less lines of codes as shown below
btnOk.setOnAction( e -> {
    // codes to handle button event would be written here

Another example can be seen in Thread code where anonymous class codes can now be substituted for that of a lambda expression
new Thread(
	() -> System.out.println("This is a seperate thread")
Furthermore, Lambda Expression can be used in collections where it can be used to simplify some operations such as filtering, sorting and even querying of items in a collection.
    ArrayList<Integer> arrayList= new ArrayList<>();
		for(int i=1; i<=100; i++)
        //without lambda expression
		for(Integer i: arrayList){
			System.out.println( i);	
        //with lambda expression
		arrayList.forEach( i -> {System.out.println(i);});	
From the codes above, it is glaring that lambda expression minimizes the lines of codes to be written, thus making it elegant. 

In this blog post, I have been able to show how Lambda Expression can be made to bridge the gap between java and functional languages, while reducing the lines of code written in the process.

From Idea to Application gives you the architecture to quickly build, manage and run a range of applications (web, mobile, big data, new smart devices, etc.) on an open-standard, cloud-based platform. See why developers are using IBM Bluemix. Brought to you in partnership with IBM.


Published at DZone with permission of Ayobami Adewole, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}