Monday , October 23 2017
Home / java8 / User defined sorting with Java 8 Comparator

User defined sorting with Java 8 Comparator

In this tutorials, we are going to see how to sort user defined objects using Java 8 comparator with lambda expression. In the previous tutorials we have discussed about what is comparator in Java,  you can seen the basic functionality of the comparator in java here.

Java 8 Comparator :

Sorting the Cricketer objects with  comparator with Java 8 extreme feature Lambda expressions.

Create Cricketer Pojo class.

Cricketer.java

class Cricketer {
    private int id;
    private String name;
    private int score;

    public Cricketer(int id, String name, int score) {
        super();
        this.id = id;
        this.name = name;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", score=" + score
                + "]";
    }

}

Comparator with Lambda :

package com.onlinetutorialspoint.java8;

import java.util.Arrays;
import java.util.List;

public class Java8_Comparator_with_Lambda {
    public static void main(String[] args) {
        List<Cricketer> cricketer = Arrays.asList(
                new Cricketer(1, "Dhoni", 102), new Cricketer(2, "Virat Kohli",
                        98), new Cricketer(3, "Rohit Sharma", 75),
                new Cricketer(4, "Youraj Singh", 84), new Cricketer(5, "Raina",
                        98));

        System.out.println("Before Sorting..");
        cricketer.forEach(System.out::println);

        sortByName(cricketer);

    }

    public static void sortByScore(List<Cricketer> crickters) {
        crickters.sort((Cricketer o1, Cricketer o2) -> o1.getScore()
                - o2.getScore());
        System.out.println("After Sorting..");
        crickters.forEach((developer) -> System.out.println(developer));
    }

    public static void sortByName(List<Cricketer> crickters) {
        crickters.sort((Cricketer c1, Cricketer c2) -> c1.getName().compareTo(
                c2.getName()));
        System.out.println("After Sorting..");
        crickters.forEach((developer) -> System.out.println(developer));
    }
}

Before Sorting..
Student [id=1, name=Dhoni, score=102] Student [id=2, name=Virat Kohli, score=98] Student [id=3, name=Rohit Sharma, score=75] Student [id=4, name=Youraj Singh, score=84] Student [id=5, name=Raina, score=98] After Sorting..
Student [id=1, name=Dhoni, score=102] Student [id=5, name=Raina, score=98] Student [id=3, name=Rohit Sharma, score=75] Student [id=2, name=Virat Kohli, score=98] Student [id=4, name=Youraj Singh, score=84]

Comparing with  Stream :


package com.onlinetutorialspoint.java8;

import java.util.Arrays;
import java.util.Comparator;
import static java.util.Comparator.*;
import java.util.List;


public class Java8_Comparator_with_Lambda {
    public static void main(String[] args) {
        List<Cricketer> cricketer = Arrays.asList(
                new Cricketer(1, "Dhoni", 102), new Cricketer(2, "Virat Kohli",
                        98), new Cricketer(3, "Rohit Sharma", 75),
                new Cricketer(4, "Youraj Singh", 84), new Cricketer(5, "Raina",
                        98));
        
        sortUsingStream(cricketer, comparing(Cricketer::getName));

    }
   
    public static void sortUsingStream(List<Cricketer> crickters,Comparator<Cricketer> comp){
        crickters.stream().sorted(comp).forEach(System.out::println);
    }
}

[/java

Here comparing() is a static method from java.util.Comparator, it accepts a function that extracts a java.lang.Comparable sort key from a type {T}, and returns a  Comparator<T> that compares by that sort key.

Student [id=1, name=Dhoni, score=102] Student [id=5, name=Raina, score=98] Student [id=3, name=Rohit Sharma, score=75] Student [id=2, name=Virat Kohli, score=98] Student [id=4, name=Youraj Singh, score=84]

Happy Learning:)

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Java 8 Read File Line By Line Example

In this tutorials, I am going to show you how to read a File line …

Leave a Reply

Your email address will not be published. Required fields are marked *