Examples of simple Lambda expressions in Java 8

Source: Internet
Author: User
Tags anonymous

I tried to integrate Lambda expressions into my code. The following code example shows the result of this attempt. For Java programmers who do not know Lambda expressions at all, I strongly recommend that you read this article before continuing to read it.

Now that you are familiar with Lambda expressions (after reading the recommended Lambda getting started article), let's start learning an example of Lambda expressions that I think are good.

Consider this scenario: some operations require preprocessing before execution, and post-processing after execution. The operation to be executed changes with the behavior. Preprocessing extracts the necessary parameters required for this operation and cleans up the process later.

Let's take a look at how to use the anonymous implementation class of interfaces to simulate this scenario.

Use an anonymous internal class


An interface implementation that provides necessary behavior methods:

The code is as follows: Copy code

Interface OldPerformer {

Public void merge mtask (String id, int status );

}




The following are some pre-processing and post-processing methods:

The code is as follows: Copy code
Public class PrePostDemo {

Static void merge mtask (String id, oldtimer Mer ){

System. out. println ("Pre-Processing ...");

System. out. println ("Fetching the status for id:" + id );

Int status = 3;

// Some status value fetched

Timer Mer. Merge mtask (id, status );

System. out. println ("Post-processing ...");

    }

}


We need to pass two things: the identifiers required for preprocessing and the implementation of operations. As follows:

The code is as follows: Copy code
Public class PrePostDemo {

Public static void main (String [] args ){

// Has to be declared final to be accessed

// The anonymous inner class.

Final String outsideOfImpl = "Common Value ";

Effecmtask ("1234", new old0000mer (){

@ Override

Public void merge mtask (String id, int status ){

System. out. println ("Finding data based on id ..."

);

System. out. println (outsideOfImpl );

System. out. println ("Asserting that the status matches ");

        }

});

Effecmtask ("4567", new old0000mer (){

@ Override

Public void merge mtask (String id, int status ){

System. out. println ("Finding data based on id ...");

System. out. println (outsideOfImpl );

System. out. println ("Update status of the data found ");

        }

});

    }

}


From the code above, we can see that variables outside the anonymous internal class must be declared as final to be accessed by the anonymous internal class. The sample code output is as follows:

The code is as follows: Copy code
Pre-Processing... </pre>
Fetching the status for id: 1234

Finding data based on id...

Common Value

Asserting that the status matches

Post-processing...

PreProcessing...

Fetching the status for id: 4567

Finding data based on id...

Common Value

Update the status of the data found

Post-processing...



Use Lambda expressions

Let's take a look at how to use Lambda expressions to implement the above example:

The code is as follows: Copy code
Public class PrePostLambdaDemo {

Public static void main (String [] args ){

// Need not be declared as final for use within

// Lambda expression, but has to be eventually final.

String outsideOfImpl = "Common Value ";

DoSomeProcessing ("123", (String id, int status)-> {

System. out. println ("Finding some data based on" + id );

System. out. println (outsideOfImpl );

System. out. println ("Assert that the status is" + status );

});

DoSomeProcessing ("456", (String id, int status)-> {

System. out. print ("Finding data based on id:" + id );

System. out. println (outsideOfImpl );

System. out. println ("And updating the status:" + status );

});

    }

Static void doSomeProcessing (String id, timer Mer extends Mer ){

System. out. println ("Pre-Processing ...");

System. out. println ("Finding status for given id:" + id );

Int status = 2;

Timer Mer. Merge mtask (id, status );

System. out. println ("Post-processing ...");

    }

}

Interface timer Mer {

Public void merge mtask (String id, int status );

}


In addition to the interesting Lambda expression syntax, there is another difference, that is, the variables outside the Lambda expression are not declared as final. But the final variable will still become a constant, which means that the value of outsideOfImpl after declaration and initialization cannot be changed.

This example only shows how to use clear Lambda expressions to replace anonymous internal classes.

A small note: the release date of JDK8 was postponed to February 2014. You can refer to the complete release schedule here. I update the Lambda build project every day. If any problem occurs in the latest build, please feel free to contact me. I will do my best to continue building and will present the latest example here.

Another tip: Do not confuse Java 8 updates. Most of the new features already exist in other programming languages. I found that learning the syntax and method of Lambda expressions can help me think in a functional way, and I should be particularly grateful for Scala closures.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.