Saturday, August 29, 2015

Java 8 Sorting using Lambda Expression example

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.


Sort using Comparator's compare method without Lambda
System.out.print("*** Normal sorting ***\n");
Arrays.sort(persons, new Comparator() {
@Override
public int compare(String s1, String s2) {
return (s1.compareTo(s2));
}
});
Arrays.asList(persons).forEach((person) -> System.out.println(person));

We can see that for simple sort, we need to write so bunch of lines. Using Lamda, we can do more in less.

Sort using Lambda - Sort by Name
System.out.print("*** Sorting usinng Lamda - SortByName ***\n");
Comparator sortbyName = (String s1,String s2) -> (s1.compareTo(s2));
Arrays.sort(persons, sortbyName);
Arrays.asList(persons).forEach((person) -> System.out.println(person));

//another way to sort using Lamda
//Arrays.sort(persons, (String s1, String s2) -> (s1.compareTo(s2)));

Sorting without Lambda - Sort By SurName
System.out.print("*** Normal sorting - sort by SureName***\n");
Arrays.sort(persons, new Comparator() {
@Override
public int compare(String s1, String s2) {
return (s1.substring(s1.indexOf(" ")).compareTo(s2.substring(s2.indexOf(" "))));
}
});
Arrays.asList(persons).forEach((person) -> System.out.println(person));

Sorting using Lambda - Sort by SurName
System.out.print("*** Sorting using Lamda - sort by SureName***\n");
Comparator sortBySurname = (String s1,String s2) -> (s1.substring(s1.indexOf(" ")).compareTo(s2.substring(s2.indexOf(" "))));
Arrays.sort(persons, sortBySurname);
Arrays.asList(persons).forEach((person) -> System.out.println(person));

//another way less code
//Arrays.sort(persons,(String s1, String s2) -> (s1.substring(s1.indexOf(" ")).compareTo(s2.substring(s2.indexOf(" ")))));

Normal sorting - sort by Length
System.out.print("*** Normal sorting - sort by Length ***\n");
Arrays.sort(persons, new Comparator() {
@Override
public int compare(String s1, String s2) {
return (s1.length() - s2.length());
}
});
Arrays.asList(persons).forEach((person) -> System.out.println(person));

Sorting using Lamda - sort by Length
System.out.print("*** Sorting using Lamda - sort by Length ***\n");
Comparator sortByNameLenght = (String s1, String s2) -> (s1.length() - s2.length());
Arrays.sort(persons, sortByNameLenght);
Arrays.asList(persons).forEach((person) -> System.out.println(person));
//another way less code
//Arrays.sort(persons, (String s1, String s2) -> (s1.length() - s2.length()));

Normal sorting - sort by Last Letter
System.out.print("*** Normal sorting - sort by Last Letter ***\n");
Arrays.sort(persons, new Comparator() {
@Override
public int compare(String s1, String s2) {
return (s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1));
}
});
Arrays.asList(persons).forEach((person) -> System.out.println(person));

Sorting using Lamda - sort by Last Letter
System.out.print("*** Sorting using Lamda - sort by Last Letter ***\n");
Comparator sortByLastLetter = (String s1,String s2) -> (s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1));
Arrays.sort(persons, sortByLastLetter);
Arrays.asList(persons).forEach((person) -> System.out.println(person));
//another way less code
//Arrays.sort(persons, (String s1, String s2) -> (s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1)));

Output
*** Before Sorting ***:
Anuj Patel
Munjal Thakkar
Jigar Shah
Bhargav Patel
Harish Raghwan
Zvika Chananel

*** Normal sorting ***
Anuj Patel
Bhargav Patel
Harish Raghwan
Jigar Shah
Munjal Thakkar
Zvika Chananel

*** Sorting using Lamda - SortByName ***
Anuj Patel
Bhargav Patel
Harish Raghwan
Jigar Shah
Munjal Thakkar
Zvika Chananel

*** Normal sorting - sort by SureName***
Zvika Chananel
Anuj Patel
Bhargav Patel
Harish Raghwan
Jigar Shah
Munjal Thakkar

*** Sorting using Lamda - sort by SureName***
Zvika Chananel
Anuj Patel
Bhargav Patel
Harish Raghwan
Jigar Shah
Munjal Thakkar

*** Normal sorting - sort by Length ***
Anuj Patel
Jigar Shah
Bhargav Patel
Zvika Chananel
Harish Raghwan
Munjal Thakkar

*** Sorting using Lamda - sort by Length ***
Anuj Patel
Jigar Shah
Bhargav Patel
Zvika Chananel
Harish Raghwan
Munjal Thakkar

*** Normal sorting - sort by Last Letter ***
Jigar Shah
Anuj Patel
Bhargav Patel
Zvika Chananel
Harish Raghwan
Munjal Thakkar

*** Sorting using Lamda - sort by Last Letter ***
Jigar Shah
Anuj Patel
Bhargav Patel
Zvika Chananel
Harish Raghwan
Munjal Thakkar

Author : Anuj Patel

No comments:

Post a Comment