Using Comparable
First we can understand what is comparable. A comparable object is capable of comparing itself with another object. Its Interface and when we want to sort any Pojo class property we need to have this interface implemented if not then there will be a class cast exception. There are some cases where the OOTB class already implemented the comparable in that case we no need to implement it . eg: string where we can directly call the sort method without implementing the comparable interface.Once we implement the Comparable<T> interface we need to override the compareTo method, T is the type. Also using the comparable we can sort only the single element or the property.
Using Comparator
When we dont want to use our pojo class implementing the Comparable interface and not to use the compartTo method and sort using the multiple property there comes the comparator.
We can see the sample to demonstrate it .
Implementing Comparable using Java 8
Student is the pojo class implementing the Comparable interface.
package com.searchendeca.samples;
public class Student implements Comparable<Student> {
public Student(String pName, String pRollNo) {
super();
mName = pName;
mRollNo = pRollNo;
}
private String mName;
private String mRollNo;
public String getName() {
return mName;
}
public void setName(String pName) {
mName = pName;
}
@Override
public String toString() {
return "Student [mName=" + mName + ", mRollNo=" + mRollNo + "]";
}
public String getRollNo() {
return mRollNo;
}
public void setRollNo(String pRollNo) {
mRollNo = pRollNo;
}
@Override
public int compareTo(Student pO) {
return this.mName.compareTo(pO.getName());
}
}
Main Method that calls this will be.
package com.searchendeca.samples;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class StudentMain {
public static void main(String args[]) {
List<Student> stuList = new ArrayList<>();
Student stu = new Student("Syed", "1001");
stuList.add(stu);
stu = new Student("Ghouse", "1002");
stuList.add(stu);
stu = new Student("Arun", "1003");
stuList.add(stu);
System.out.print("Before Sorting");
stuList.stream().forEach(System.out::println);
Collections.sort(stuList);
System.out.print("After Sorting");
stuList.stream().forEach(System.out::println);
Collections.reverse(stuList);
System.out.print("After Reveresing");
stuList.stream().forEach(System.out::println);
}
}
Output:
Before SortingStudent [mName=Syed, mRollNo=1001]
Student [mName=Ghouse, mRollNo=1002]
Student [mName=Arun, mRollNo=1003]
After SortingStudent [mName=Arun, mRollNo=1003]
Student [mName=Ghouse, mRollNo=1002]
Student [mName=Syed, mRollNo=1001]
After ReveresingStudent [mName=Syed, mRollNo=1001]
Student [mName=Ghouse, mRollNo=1002]
Student [mName=Arun, mRollNo=1003]
Implementing Comparator using Java 8
package com.searchendeca.samples;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class StudentMain {
public static void main(String args[]) {
List<Student> stuList = new ArrayList<>();
Student stu = new Student("Syed", "1001");
stuList.add(stu);
stu = new Student("Arun", "1003");
stuList.add(stu);
stu = new Student("Ghouse", "1002");
stuList.add(stu);
System.out.println("Before Sorting");
stuList.stream().forEach(System.out::println);
System.out.println("After Sorting using RollNo");
stuList=sortUsingRollNo(stuList);
stuList.stream().forEach(System.out::println);
System.out.println("After Sorting using Name");
stuList=sortUsingName(stuList);
stuList.stream().forEach(System.out::println);
}
static List<Student> sortUsingRollNo(List<Student> stuList) {
Collections.sort(stuList, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getRollNo().compareTo(s2.getRollNo());
}
});
return stuList;
}
static List<Student> sortUsingName(List<Student> stuList) {
Collections.sort(stuList, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName());
}
});
return stuList;
}
}
Output::
Before Sorting
Student [mName=Syed, mRollNo=1001]
Student [mName=Arun, mRollNo=1003]
Student [mName=Ghouse, mRollNo=1002]
After Sorting using RollNo
Student [mName=Syed, mRollNo=1001]
Student [mName=Ghouse, mRollNo=1002]
Student [mName=Arun, mRollNo=1003]
After Sorting using Name
Student [mName=Arun, mRollNo=1003]
Student [mName=Ghouse, mRollNo=1002]
Student [mName=Syed, mRollNo=1001]
Applying the lamda expressions to it will change in the below way.
package com.searchendeca.samples;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class StudentMain {
public static void main(String args[]) {
List<Student> stuList = new ArrayList<>();
Student stu = new Student("Syed", "1001");
stuList.add(stu);
stu = new Student("Arun", "1003");
stuList.add(stu);
stu = new Student("Ghouse", "1002");
stuList.add(stu);
System.out.println("Before Sorting");
stuList.stream().forEach(System.out::println);
System.out.println("After Sorting using RollNo");
stuList=sortUsingRollNo(stuList);
stuList.stream().forEach(System.out::println);
System.out.println("After Sorting using Name");
stuList=sortUsingName(stuList);
stuList.stream().forEach(System.out::println);
}
static List<Student> sortUsingRollNo(List<Student> stuList) {
Collections.sort(stuList, (c1,c2)->{
return c1.getRollNo().compareTo(c1.getRollNo());
}
);
return stuList;
}
static List<Student> sortUsingName(List<Student> stuList) {
Collections.sort(stuList, (c1,c2)->{
return c1.getName().compareTo(c1.getName());
}
);
return stuList;
}
}
Output::
Before Sorting
Student [mName=Syed, mRollNo=1001]
Student [mName=Arun, mRollNo=1003]
Student [mName=Ghouse, mRollNo=1002]
After Sorting using RollNo
Student [mName=Syed, mRollNo=1001]
Student [mName=Arun, mRollNo=1003]
Student [mName=Ghouse, mRollNo=1002]
After Sorting using Name
Student [mName=Syed, mRollNo=1001]
Student [mName=Arun, mRollNo=1003]
Student [mName=Ghouse, mRollNo=1002]
Happy Learning !!
No comments:
Write comments