Java Object Sorting using Comparator vs Comparable interface Class.
Java Sorting Comparator vs Comparable interface .
Here we are using for Collections Methods for Sorting Purpose. and ArrayList for Storing Purpose.....
Write a java program to use the Comparator and Comparable interface in Java.....
(1) Employee.java
package ComparatorVsComparable;
import java.util.Comparator;
public class Employee {
private String empNameId;
private int empAge;
public Employee() {
}
Employee(String empNameId , int empAge) {
this.empNameId = empNameId ;
this.empAge = empAge ;
}
public String getName() {
return empNameId;
}
public void setName(String empNameId) {
this.empNameId = empNameId;
}
public int getAge() {
return empAge;
}
public void setAge(int empAge) {
this.empAge = empAge;
}
}
(2)CompareClassOnly.java
package ComparatorVsComparable;
import java.util.Comparator;
public class CompareClassOnly implements Comparator<Employee> {
@Override
public int compare(Employee emp1, Employee emp2) { // Compare Numbers
System.out.println(" -----------CompareClassOnly Class Executed with Compare Method -----------");
return emp1.getAge() > emp2.getAge() ? -1 : (emp1.getAge() == emp2.getAge() ? 0 : 1 );
}
}
(3)CompareStringOnly.java
package ComparatorVsComparable;
import java.util.Comparator;
public class CompareStringOnly implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
System.out.println(" -----------CompareStringOnly Class Executed with Compare Method -----------");
String str1 = o1.getName();
String str2 = o2.getName();
return str1.compareTo(str2);
}
}
(4) ComparatorMainClass.java
package ComparatorVsComparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ComparatorMainClass {
public static void main(String args[]) {
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee("Hari581", 24));
list.add(new Employee("Dayanidhi582", 23));
list.add(new Employee("Athik111", 22));
list.add(new Employee("Premkumar486", 28));
list.add(new Employee("Muthu420", 27));
Collections.sort(list , new CompareStringOnly());
System.out.println(" \n After Collection Default Descending Sorting NameWise Order Details : ");
for(Employee e : list) {
System.out.println(" EmpNameId : " +e.getName()+" EmpAge : " + e.getAge());
}
Collections.sort(list , new CompareClassOnly());
System.out.println(" \n After Collection Default Descending Sorting AgeWise Order Details : ");
for(Employee e : list) {
System.out.println(" EmpNameId : " +e.getName()+" EmpAge : " + e.getAge());
}
}
}
In This Example used to Find the Given Name with Age for Sorting Purpose..
First Sort with Name Wise ( String Sorting )
Second Sort With Age Wise ( Number Sorting)
output::
-----------CompareStringOnly Class Executed with Compare Method -----------
-----------CompareStringOnly Class Executed with Compare Method -----------
-----------CompareStringOnly Class Executed with Compare Method -----------
-----------CompareStringOnly Class Executed with Compare Method -----------
-----------CompareStringOnly Class Executed with Compare Method -----------
-----------CompareStringOnly Class Executed with Compare Method -----------
After Collection Default Descending Sorting NameWise Order Details :
EmpNameId : Athik111 EmpAge : 22
EmpNameId : Dayanidhi582 EmpAge : 23
EmpNameId : Hari581 EmpAge : 24
EmpNameId : Muthu420 EmpAge : 27
EmpNameId : Premkumar486 EmpAge : 28
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
After Collection Default Descending Sorting AgeWise Order Details :
EmpNameId : Premkumar486 EmpAge : 28
EmpNameId : Muthu420 EmpAge : 27
EmpNameId : Hari581 EmpAge : 24
EmpNameId : Dayanidhi582 EmpAge : 23
EmpNameId : Athik111 EmpAge : 22
Note:: This Program is for mm Own Code.. If you have any Query pls mail to: vhkrishnan.v@gmail.com
Java Sorting Comparator vs Comparable interface .
Here we are using for Collections Methods for Sorting Purpose. and ArrayList for Storing Purpose.....
Write a java program to use the Comparator and Comparable interface in Java.....
(1) Employee.java
package ComparatorVsComparable;
import java.util.Comparator;
public class Employee {
private String empNameId;
private int empAge;
public Employee() {
}
Employee(String empNameId , int empAge) {
this.empNameId = empNameId ;
this.empAge = empAge ;
}
public String getName() {
return empNameId;
}
public void setName(String empNameId) {
this.empNameId = empNameId;
}
public int getAge() {
return empAge;
}
public void setAge(int empAge) {
this.empAge = empAge;
}
}
(2)CompareClassOnly.java
package ComparatorVsComparable;
import java.util.Comparator;
public class CompareClassOnly implements Comparator<Employee> {
@Override
public int compare(Employee emp1, Employee emp2) { // Compare Numbers
System.out.println(" -----------CompareClassOnly Class Executed with Compare Method -----------");
return emp1.getAge() > emp2.getAge() ? -1 : (emp1.getAge() == emp2.getAge() ? 0 : 1 );
}
}
(3)CompareStringOnly.java
package ComparatorVsComparable;
import java.util.Comparator;
public class CompareStringOnly implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
System.out.println(" -----------CompareStringOnly Class Executed with Compare Method -----------");
String str1 = o1.getName();
String str2 = o2.getName();
return str1.compareTo(str2);
}
}
(4) ComparatorMainClass.java
package ComparatorVsComparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ComparatorMainClass {
public static void main(String args[]) {
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee("Hari581", 24));
list.add(new Employee("Dayanidhi582", 23));
list.add(new Employee("Athik111", 22));
list.add(new Employee("Premkumar486", 28));
list.add(new Employee("Muthu420", 27));
Collections.sort(list , new CompareStringOnly());
System.out.println(" \n After Collection Default Descending Sorting NameWise Order Details : ");
for(Employee e : list) {
System.out.println(" EmpNameId : " +e.getName()+" EmpAge : " + e.getAge());
}
Collections.sort(list , new CompareClassOnly());
System.out.println(" \n After Collection Default Descending Sorting AgeWise Order Details : ");
for(Employee e : list) {
System.out.println(" EmpNameId : " +e.getName()+" EmpAge : " + e.getAge());
}
}
}
In This Example used to Find the Given Name with Age for Sorting Purpose..
First Sort with Name Wise ( String Sorting )
Second Sort With Age Wise ( Number Sorting)
output::
-----------CompareStringOnly Class Executed with Compare Method -----------
-----------CompareStringOnly Class Executed with Compare Method -----------
-----------CompareStringOnly Class Executed with Compare Method -----------
-----------CompareStringOnly Class Executed with Compare Method -----------
-----------CompareStringOnly Class Executed with Compare Method -----------
-----------CompareStringOnly Class Executed with Compare Method -----------
After Collection Default Descending Sorting NameWise Order Details :
EmpNameId : Athik111 EmpAge : 22
EmpNameId : Dayanidhi582 EmpAge : 23
EmpNameId : Hari581 EmpAge : 24
EmpNameId : Muthu420 EmpAge : 27
EmpNameId : Premkumar486 EmpAge : 28
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
-----------CompareClassOnly Class Executed with Compare Method -----------
After Collection Default Descending Sorting AgeWise Order Details :
EmpNameId : Premkumar486 EmpAge : 28
EmpNameId : Muthu420 EmpAge : 27
EmpNameId : Hari581 EmpAge : 24
EmpNameId : Dayanidhi582 EmpAge : 23
EmpNameId : Athik111 EmpAge : 22
Note:: This Program is for mm Own Code.. If you have any Query pls mail to: vhkrishnan.v@gmail.com
Tittanium Arcton
ReplyDeleteTittanium Art - Tittanium. titanium trim reviews Tittanium Art. Tittanium Art. Tittanium Art. Tittanium Art. Tittanium Art. Tittanium Art. Tittanium titanium fat bike Art. Tittanium Art. Tittanium micro touch titanium trim Art. Tittanium titanium stud earrings Art. Tittanium Art. Tittanium Art. titanium joes Tittanium Art. Tittanium Art.