Step 1: Create a Bean class
public class CommonPojo {
private int empId;
private String empName;
public CommonPojo() {
// TODO Auto-generated constructor stub
}
public CommonPojo(int id, String name) {
this.empId=id;
this.empName= name;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
Step 2: Writing an UniversalSorting class to sort the objects
import java.util.Comparator;
public class UniversalSorting {
public static enum CommonSort implements Comparator<CommonPojo> {
BY_NAME {
@Override
public int compare(CommonPojo commonPojo1, CommonPojo commonPojo2) {
return commonPojo1.getEmpName().compareTo(commonPojo2.getEmpName());
}
},
BY_ID {
@Override
public int compare(CommonPojo commonPojo1, CommonPojo commonPojo2) {
return commonPojo1.getEmpId() - commonPojo2.getEmpId();
}
}
}
}
Step 3: Create an universal method in TestCommonSort class to sort the objects and test it
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestCommonSort {
public static void main(String[] args) {
sortEmployee(UniversalSorting.CommonSort.BY_NAME);
//sortEmployee(UniversalSorting.CommonSort.BY_ID);
}
public static void sortEmployee(UniversalSorting.CommonSort sorting){
try{
List<CommonPojo> listPojo= new ArrayList<CommonPojo>();
listPojo.add(new CommonPojo(1, "Sumit"));
listPojo.add(new CommonPojo(1, "Kuldeep"));
listPojo.add(new CommonPojo(1, "Sanjay"));
listPojo.add(new CommonPojo(31, "Umesh"));
listPojo.add(new CommonPojo(4, "Abhishek"));
listPojo.add(new CommonPojo(6, "Santosh"));
listPojo.add(new CommonPojo(7, "Bharat"));
listPojo.add(new CommonPojo(88, "Dipak"));
Collections.sort(listPojo, sorting);
for(CommonPojo commonPojo: listPojo){
System.out.println(commonPojo.getEmpName());
}
} catch (Exception ex){
ex.printStackTrace();
}
}
}
Step 4: Output
Bharat
Dipak
Kuldeep
Sanjay
Santosh
Sumit
Umesh
public class CommonPojo {
private int empId;
private String empName;
public CommonPojo() {
// TODO Auto-generated constructor stub
}
public CommonPojo(int id, String name) {
this.empId=id;
this.empName= name;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
Step 2: Writing an UniversalSorting class to sort the objects
import java.util.Comparator;
public class UniversalSorting {
public static enum CommonSort implements Comparator<CommonPojo> {
BY_NAME {
@Override
public int compare(CommonPojo commonPojo1, CommonPojo commonPojo2) {
return commonPojo1.getEmpName().compareTo(commonPojo2.getEmpName());
}
},
BY_ID {
@Override
public int compare(CommonPojo commonPojo1, CommonPojo commonPojo2) {
return commonPojo1.getEmpId() - commonPojo2.getEmpId();
}
}
}
}
Step 3: Create an universal method in TestCommonSort class to sort the objects and test it
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestCommonSort {
public static void main(String[] args) {
sortEmployee(UniversalSorting.CommonSort.BY_NAME);
//sortEmployee(UniversalSorting.CommonSort.BY_ID);
}
public static void sortEmployee(UniversalSorting.CommonSort sorting){
try{
List<CommonPojo> listPojo= new ArrayList<CommonPojo>();
listPojo.add(new CommonPojo(1, "Sumit"));
listPojo.add(new CommonPojo(1, "Kuldeep"));
listPojo.add(new CommonPojo(1, "Sanjay"));
listPojo.add(new CommonPojo(31, "Umesh"));
listPojo.add(new CommonPojo(4, "Abhishek"));
listPojo.add(new CommonPojo(6, "Santosh"));
listPojo.add(new CommonPojo(7, "Bharat"));
listPojo.add(new CommonPojo(88, "Dipak"));
Collections.sort(listPojo, sorting);
for(CommonPojo commonPojo: listPojo){
System.out.println(commonPojo.getEmpName());
}
} catch (Exception ex){
ex.printStackTrace();
}
}
}
Step 4: Output
Bharat
Dipak
Kuldeep
Sanjay
Santosh
Sumit
Umesh
nice blog
ReplyDeleteThank You
DeleteThanks for explaining.............
ReplyDeletebut what are means by hashcode and equals if u use two list for
sorting .
i want remove duplicate only one collection like list..etc