I was late trying out my first lambda expressions !!!!! But still I will give a try to sort the names using the Last Name. Here is my Code.
package com.mycommercesearch.solr;
public class Person {
private String firstName;
private String lastName;
private int age;
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";
}
public Person(String firstName, String lastName, int age) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.mycommercesearch.solr;
import java.util.List;
@FunctionalInterface
public interface NameSorter {
void soryByLastName(List<Person> person,String arrangement);
}
Step:1 Create a Pojo Class
FileName:Person.javapackage com.mycommercesearch.solr;
public class Person {
private String firstName;
private String lastName;
private int age;
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";
}
public Person(String firstName, String lastName, int age) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Step:2 Create Interface NameSorter
FileName:NameSorter.javapackage com.mycommercesearch.solr;
import java.util.List;
@FunctionalInterface
public interface NameSorter {
void soryByLastName(List<Person> person,String arrangement);
}
Step:3 Create a main class
FileName:NameTest.java
package com.mycommercesearch.solr;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class NameTest{
public void invokeSample(NameSorter nameTest,List<Person> person,String arrangement) {
nameTest.soryByLastName(person,arrangement);
}
public static void main(String args[]) {
//Here I have Initialized Person array with custom values.
List<Person> myPerson = Arrays.asList(new Person("Syed", "Ghouse", 27),
new Person("Manoj", "Kumar", 45),new Person("Chetan", "Bagath", 50),new Person("Eddapadi", "Palaniswamy", 60),new Person("Paneer", "Selvam", 55));
//Here I have written the lamda expression to print the List
SamplerTest sortName = (person,arrangeMent) -> {
if(!person.isEmpty()) {
person.forEach(ps-> System.out.println("Sorting using the last Name "+arrangeMent+"::"+ps.getFirstName()+" "+ps.getLastName()));
}
};
//Creating the object for my class
NameTest nameTest= new NameTest();
//Passing the behaviour to my interface
nameTest.invokeSample(sortName,myPerson,"Before");
//Invoking the sort by LastName
nameTest.sortLastName(myPerson);
//Passing the sorted bahviuor back to my previous lamda
nameTest.invokeSample(sortName,myPerson,"after");
}
public void sortLastName(List<Person> pes) {
Collections.sort(pes,(Person o1,Person o2)->{
Person p1= (Person)o1;
Person p2=(Person) o2;
return p1.getLastName().compareTo(p2.getLastName());
});
}
}
Output will be in the following format:
Sorting using the last Name Before::Syed Ghouse
Sorting using the last Name Before::Manoj Kumar
Sorting using the last Name Before::Chetan Bagath
Sorting using the last Name Before::Eddapadi Palaniswamy
Sorting using the last Name Before::Paneer Selvam
Sorting using the last Name after::Chetan Bagath
Sorting using the last Name after::Syed Ghouse
Sorting using the last Name after::Manoj Kumar
Sorting using the last Name after::Eddapadi Palaniswamy
Sorting using the last Name after::Paneer Selvam
We can look more on how to use the lamda expressions on our daily coding ways in the upcoming posts.
Happy Expressions!!!!
No comments:
Write comments