Showing posts with label java8. Show all posts
Showing posts with label java8. Show all posts

Saturday, August 29, 2015

Java 8 Sorting using Lambda Expression example

Links to this post
I have array containing names of my friends. I want to sort those name so I can get my friends's name sorted  by name alphabetically.

Persons Array
String[] persons = {"Anuj Patel", "Munjal Thakkar", "Jigar Shah", "Bhargav Patel", "Harish Raghwan","Zvika Chananel"};

Print Array using Lambda Expressions
System.out.print("*** Before Sorting ***:\n");
Arrays.asList(persons).forEach((person) -> System.out.println(person));

Here, we are creating List from Array and saying that for each person, print person.
Lambda expression is (Parameter) -> Body in simple words. It's more readable form.

Java 8 Threading Using Lambda expression Example

Links to this post
As we already know that In Java, we can create thread using two ways - using extends and another is using Runnable.

Normally we create Thread using below ways.
Thread t1 = new Thread(new Runnable() {
public void run() {
System.out.println("Threading Using old way !");

We write codes because we are interested in doing something. we write something and expecting some output. We are more interested in outcome that our code is supposed to provide. here we expect our code to print "Threading Using old way!" once it runs.
But It seems we are writing extra codes for simple thing !