Comparator and Comparable in java with example

Comparable and comparator interfaces are provided by Java for sorting the collection of objects.In this tutorial, we will see Comparator and Comparable in java with the example for  sorting Java

Sort using Comparable interface

comparable and comparator in JavaComparable is provided by java.lang package and is used for sorting the object on the single attribute.Comparable provides compareTo() method to be implemented for sorting the object.


Below is the example for sorting student object based on student age using comparable interface.

Student.java

package com.javainstance.sort;

/**
 * @author javainstance
 *
 */
public class Student implements Comparable<Student> {

private int StudentId;

private String StudentName;

private int studentAge;

public Student(int studentId, String studentName, int studentAge) {
super();
StudentId = studentId;
StudentName = studentName;
this.studentAge = studentAge;
}

public int getStudentId() {
return StudentId;
}

public void setStudentId(int studentId) {
StudentId = studentId;
}

public String getStudentName() {
return StudentName;
}

public void setStudentName(String studentName) {
StudentName = studentName;
}

public int getStudentAge() {
return studentAge;
}

public void setStudentAge(int studentAge) {
this.studentAge = studentAge;
}

@Override
public String toString() {
return "Student [StudentId=" + StudentId + ", StudentName=" + StudentName + ", studentAge=" + studentAge + "]";
}

@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
return this.studentAge - o.studentAge;
}

}


SortTest.java

package com.javainstance.sort;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @author javainstance
 *
 */
public class SortTest {

public static void main(String[] args) {

ArrayList<Student> studentList = new ArrayList<>();

Student student1 = new Student(1, "John", 28);
Student student2 = new Student(2, "Aish", 20);
Student student3 = new Student(3, "Patrik", 26);

studentList.add(student1);
studentList.add(student2);
studentList.add(student3);

Collections.sort(studentList);

studentList.forEach(student -> {
System.out.println(student);

});

}


}

Output:

Student [StudentId=2, StudentName=Aish, studentAge=20]
Student [StudentId=3, StudentName=Patrik, studentAge=26]

Student [StudentId=1, StudentName=John, studentAge=28]


Sort using Comparator interface

Comparator is provided by java.util package and is used for sorting the object based on the multiple  attributes.Comparator provides compare() method which is to be implemented by separate class for sorting the object based on different parameters.

Below is the example for sorting student object based on student age and student name using comparator interface.

Student.java

package com.javainstance.sort;

/**
 * @author javainstance
 *
 */
public class Student {

private int StudentId;

private String StudentName;

private int studentAge;

public Student(int studentId, String studentName, int studentAge) {
super();
StudentId = studentId;
StudentName = studentName;
this.studentAge = studentAge;
}

public int getStudentId() {
return StudentId;
}

public void setStudentId(int studentId) {
StudentId = studentId;
}

public String getStudentName() {
return StudentName;
}

public void setStudentName(String studentName) {
StudentName = studentName;
}

public int getStudentAge() {
return studentAge;
}

public void setStudentAge(int studentAge) {
this.studentAge = studentAge;
}

@Override
public String toString() {
return "Student [StudentId=" + StudentId + ", StudentName=" + StudentName + ", studentAge=" + studentAge + "]";
}




}

AgeSorter.java

package com.javainstance.sort;

import java.util.Comparator;

/**
 * @author javainstance
 *
 */
public class AgeSorter implements Comparator<Student> {

@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.getStudentAge() - o2.getStudentAge();
}


}


NameSorter.java

package com.javainstance.sort;

import java.util.Comparator;

/**
 * @author javainstance
 *
 */
public class NameSorter implements Comparator<Student> {

@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o1.getStudentName().compareToIgnoreCase(o2.getStudentName());
}

}

SortTest.java

package com.javainstance.sort;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @author javainstance
 *
 */
public class SortTest {

public static void main(String[] args) {

ArrayList<Student> studentList = new ArrayList<>();

Student student1 = new Student(1, "John", 28);
Student student2 = new Student(2, "Aish", 20);
Student student3 = new Student(3, "Patrik", 26);

studentList.add(student1);
studentList.add(student2);
studentList.add(student3);

// Sort By Name

Collections.sort(studentList, new NameSorter());

studentList.forEach(student -> {
System.out.println(student);

});

System.out.println();
// Sort by Age

Collections.sort(studentList, new AgeSorter());

studentList.forEach(student -> {
System.out.println(student);

});

}


}


Output:

Student [StudentId=2, StudentName=Aish, studentAge=20]
Student [StudentId=1, StudentName=John, studentAge=28]
Student [StudentId=3, StudentName=Patrik, studentAge=26]

Student [StudentId=2, StudentName=Aish, studentAge=20]
Student [StudentId=3, StudentName=Patrik, studentAge=26]
Student [StudentId=1, StudentName=John, studentAge=28]

Using Comparator in Java 8


package com.javainstance.sort;

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

/**
 * @author javainstance
 *
 */
public class SortTest {

public static void main(String[] args) {

List<Student> studentList = new ArrayList<>();

Student student1 = new Student(1, "John", 28);
Student student2 = new Student(2, "Aish", 20);
Student student3 = new Student(3, "Patrik", 26);
Student student4 = new Student(4, "Patrik", 24);

studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
studentList.add(student4);


// Sort By Name-Using Comparing
studentList.sort(Comparator.comparing(Student::getStudentName));

studentList.forEach(student -> {
System.out.println(student);

});

System.out.println();

// Sort by Age-Using Comparing

studentList.sort(Comparator.comparing(Student::getStudentAge));

studentList.forEach(student -> {
System.out.println(student);

});
System.out.println();
//Sort by Age-Using Lambda Expression
studentList.sort((Student s1, Student s2)->s1.getStudentAge()-s2.getStudentAge());
studentList.forEach(student -> {
System.out.println(student);

});
System.out.println();

// Sort on multiple fields
Comparator<Student> nameageComparator = Comparator.comparing(Student::getStudentName)
                                                   .thenComparing(Student::getStudentAge);
studentList.sort(nameageComparator);

studentList.forEach(student -> {
System.out.println(student);

});

}

}

Output:

Student [StudentId=2, StudentName=Aish, studentAge=20]
Student [StudentId=1, StudentName=John, studentAge=28]
Student [StudentId=3, StudentName=Patrik, studentAge=26]
Student [StudentId=4, StudentName=Patrik, studentAge=24]

Student [StudentId=2, StudentName=Aish, studentAge=20]
Student [StudentId=4, StudentName=Patrik, studentAge=24]
Student [StudentId=3, StudentName=Patrik, studentAge=26]
Student [StudentId=1, StudentName=John, studentAge=28]

Student [StudentId=2, StudentName=Aish, studentAge=20]
Student [StudentId=4, StudentName=Patrik, studentAge=24]
Student [StudentId=3, StudentName=Patrik, studentAge=26]
Student [StudentId=1, StudentName=John, studentAge=28]

Student [StudentId=2, StudentName=Aish, studentAge=20]
Student [StudentId=1, StudentName=John, studentAge=28]
Student [StudentId=4, StudentName=Patrik, studentAge=24]
Student [StudentId=3, StudentName=Patrik, studentAge=26]

References:
https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

Date and Time operation in Java

Give your suggestion in comments.
If you like the post Share with your friends on social network.












Comparator and Comparable in java with example Comparator and Comparable in java with example Reviewed by JavaInstance on 10:08:00 AM Rating: 5

1 comment:

Powered by Blogger.