Friday 2 June 2017

Lambda expression in Java[How to sort the list of developers using lambda expression and comparator]


Click here to watch in Youtube :
https://www.youtube.com/watch?v=5-APCfZqZ-w&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Lambda expression in Java[How to sort the list of developers using lambda expression and comparator] 
Developer.java
public class Developer
{
    private String name;
    private int age;

    public Developer(String name, int age)
    {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    @Override
    public String toString()
    {
        return "Developer [name=" + name + ", age=" + age + "]";
    }

}
NonLambdaDemo.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Sort without Lambda,Before JDK 8
 */
public class NonLambdaDemo
{
    public static void main(String[] args)
    {

        List<Developer> developerList = getDevelopers();

        System.out.println("-----------Before Sort-----------");
        printDeveloperInfo(developerList);

        // sort by age
        Collections.sort(developerList, new Comparator<Developer>()
        {
            @Override
            public int compare(Developer o1, Developer o2)
            {
                return o1.getAge() - o2.getAge();
            }
        });

        System.out.println("\n-----------After Sort by Age------------");
        printDeveloperInfo(developerList);

        // sort by name
        Collections.sort(developerList, new Comparator<Developer>()
        {
            @Override
            public int compare(Developer o1, Developer o2)
            {
                return o1.getName().compareTo(o2.getName());
            }
        });

        System.out.println("\n-----------After Sort by Name------------");
        printDeveloperInfo(developerList);

    }

    private static void printDeveloperInfo(List<Developer> developerList)
    {
        for (Developer developer : developerList)
        {
            System.out.println(developer);
        }
    }

    private static List<Developer> getDevelopers()
    {

        List<Developer> developerList = new ArrayList<Developer>();

        developerList.add(new Developer("Peter", 33));
        developerList.add(new Developer("John", 22));
        developerList.add(new Developer("Ram", 15));
        developerList.add(new Developer("Steve", 55));

        return developerList;

    }
}
Output
-----------Before Sort-----------
Developer [name=Peter, age=33]
Developer [name=John, age=22]
Developer [name=Ram, age=15]
Developer [name=Steve, age=55]

-----------After Sort by Age------------
Developer [name=Ram, age=15]
Developer [name=John, age=22]
Developer [name=Peter, age=33]
Developer [name=Steve, age=55]

-----------After Sort by Name------------
Developer [name=John, age=22]
Developer [name=Peter, age=33]
Developer [name=Ram, age=15]
Developer [name=Steve, age=55]
LambdaDemo.java
import java.util.ArrayList;
import java.util.List;

/**
 * Sort with Lambda,With JDK 8
 */
public class LambdaDemo
{
    public static void main(String[] args)
    {

        List<Developer> developerList = getDevelopers();

        System.out.println("-----------Before Sort-----------");
        for (Developer developer : developerList)
        {
            System.out.println(developer);
        }

        System.out.println("\n-----------After Sort by Age------------");
        // lambda here...
        developerList.sort((Developer o1, Developer o2) -> o1.getAge() - o2.getAge());

        // java 8 only, lambda also, to print the List
        developerList.forEach((developer) -> System.out.println(developer));

        System.out.println("\n-----------After Sort by Name------------");
        // lambda
        developerList.sort((Developer o1, Developer o2) -> o1.getName().compareTo(o2.getName()));

        // java 8 only, lambda also, to print the List
        developerList.forEach((developer) -> System.out.println(developer));

    }

    private static List<Developer> getDevelopers()
    {

        List<Developer> developerList = new ArrayList<Developer>();

        developerList.add(new Developer("Peter", 33));
        developerList.add(new Developer("John", 22));
        developerList.add(new Developer("Ram", 15));
        developerList.add(new Developer("Steve", 55));

        return developerList;

    }
}
Output
-----------Before Sort-----------
Developer [name=Peter, age=33]
Developer [name=John, age=22]
Developer [name=Ram, age=15]
Developer [name=Steve, age=55]

-----------After Sort by Age------------
Developer [name=Ram, age=15]
Developer [name=John, age=22]
Developer [name=Peter, age=33]
Developer [name=Steve, age=55]

-----------After Sort by Name------------
Developer [name=John, age=22]
Developer [name=Peter, age=33]
Developer [name=Ram, age=15]
Developer [name=Steve, age=55]
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LambdaDemo_with_comparator_app.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/LambdaDemo_with_comparator_app

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/b58cad09932cc81678f7144749cb560485c5bdd8/BasicJava/LambdaDemo_with_comparator_app/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment