Skip to main content

Lambda expressions in JAVA


The Best new feature of JAVA SE 8 is called project Lambda. This brings Java into the world of functional programming. In computer science terminology Lambda is an anonymous function, that is a function without a name. In Java all functions are members of classes and refer to as methods. A Lambda expressions let you define a class and a single method with very consisted task, like implementing an interface that has abstract method. It lets developers simplify and shorten their code making it more readerable and maintaiable. Lets take look at few code snippets.

Priority it Java SE 8, if you want to create thread you first define a class and implements the runable insterface. This is the interface have single abstarct method named run and accpect no arguments. You might define the class in its own code file. File named MyRunnable.java and you might named and impement the single abstract method.

public class MyRunnable impments Runnable{
@overide
public void run(){
          System.out.println("Hello");
     }
}

In the example out a string to the console. You will take that object and pass it to a instence of the tread class and calling the tread start method.

MyRunnable r = new MyRunnable();
new Thread(r).start();

My code will run its own tread and on its own memory space. In Java SE 8 you can improve this code little bit instend of decaring your class in a separate file, you might decare it as single user class know as inner class. local to methods in use.

But we can improve it more by decaring the class as Anoymous class. We will impment the run method and still calling start method.

new Thread(new Runnable(){
    @overide
    public void run(){
         System.out.println("Hello World");
    }
}).start();

In Java SE 8 we can singnicatly refacer the code that make the code lot mroe readable. The Lambda verstion look like this.

Runable r = ()-> System.out.println("Hello");
new Thred(r).start();

In the above code I am using a single line of code to decalare method implemetation and once again I am passing the object. to the Treads constrcuter.




Comments

Popular posts from this blog

Getting Started with Git & GittHub

Creating a Git Project Git is a version control system that keeps track of the files in a project. In order to use git, we start by identifying a folder as a git repository. we do it by running init command in the folder. Before running the git command go to your project folder and create two files called file1.txt and file2.txt and add some sample text to it and save the file. Now open up the git command line in the project folder and type the following command. An easy way to is by right clicking anywhere inside your project folder and click git bash here. $ git init Checking Status Now we created a git repository. Now we can check the status of our repository by typing git status in the git terminal. $ git status You can see there are two files in red which called as untracked files. This is because we still didn't add any file to our git repository. Adding files to Git repository To add files to your git repository by simply typing git add file1.t

Introduction to MEAN Stack

MEAN stands for Mongo, Express, Angular and Node. In the world of modern software, we typically deal with frontend and backend. MEAN Stack includes both technologies for both sides frontend and backend. Mongo Mongo is a cross-platform document-oriented database. Meaning is not like more traditional SQL database. Which is where NoSQL term comes from, with that means Mongo DB Store JSON-like documents with dynamic schemas making it better and faster for some applications.Companies with large scale deployments of Mongo DB includes Adobe, eBay, LinkedIn and much more. There is no database we can call as the best. It depends on the type of the system. Mongo DB is one in many options. Mongo DB is good for developers who is familiar with JavaScript and JSON and also easy to start learning. Express Express or ExpresJS is Node JS web application framework. It's the server framework for NodeJS. It allows quick development of robust web APIs by providing the thin and performa

Unit Testing with Mocha

Unit testing is the best way to uncover hidden bugs in your system. JavaScript Community has produced several testing frameworks that will help us with the task of writing Unit Tests. Some of them are Jasmin, Mocha, Jaster, Quint. These are all JavaScript Testing frameworks, that you can use to test JavaScript Code. Today we are going to focus on the Mocha testing framework. Let's go and install Mocha globally that we can use on any of our projects. $ npm install  -g mocha To run a Mocha test all you need is to type mocha. But you need to have directory named test. Before running mocha first make sure you have created a directory call test. $ mocha Now Mocha is ready to use. Now let's go and create our first test. Create a folder MochaTesting anywhere in your folder. This is the folder we are going to work. Inside that folder create a folder called test. Inside the test, folder create a file call my-test.js We are going to use Test Driven Development. That mean