/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author sowjanya.g
*/
import java.util.Arrays;
import java.util.Comparator;
/*
Java Comparator example.
This Java Comparator example describes how java.util.Comparator
interface is implemented to compare Java user defined classe's objects.
These Java Comparator is passed to Collection's sorting
method ( for example Collections.sort method)to perform sorting of Java user defined classe's objects.
*/
/*
java.util.Comparator interface declares two methods,
1) public int compare(Object object1, Object object2) and
2) boolean equals(Object object)
*/
/*
We will compare objects of the Person class using custom comparators
on the basis of person age and name.
*/
class Person{
private int age;
private String name;
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
}
/*
User defined java comaprator.
To create custom java comparator Implement Comparator interface and
define compare method.
The below given comparator compares persons on the basis of their age.
*/
class AgeComparator implements Comparator{
public int compare(Object emp1, Object emp2){
//parameter are of type Object, so we have to downcast it to Person objects
int emp1Age = ( (Person) emp1).getAge();
int emp2Age = ( (Person) emp2).getAge();
if( emp1Age > emp2Age ){
return 1;
}
else if( emp1Age < emp2Age ){
return -1;
}
else{
return 0;
}
}
}
/* The below given comparator compares persons on the basis of their name. */
class NameComparator implements Comparator{
public int compare(Object emp1, Object emp2){
//parameter are of type Object, so we have to downcast it to Person objects
String emp1Name = ( (Person) emp1 ).getName();
String emp2Name = ( (Person) emp2 ).getName();
//uses compareTo method of String class to compare names of the person
return emp1Name.compareTo(emp2Name);
}
}
/*
This Java comparator example compares persons on the basis of
their age and name and sort it in that order.
*/
public class JavaComparatorExample{
public static void main(String args[]){
/* Person array which will hold persons */
Person person[] = new Person[2];
//set different attributes of the individual person.
person[0] = new Person();
person[0].setAge(40);
person[0].setName("Joe");
person[1] = new Person();
person[1].setAge(20);
person[1].setName("Mark");
System.out.println("Order of person before sorting is");
//print array as is.
for(int i=0; i < person.length; i++){
System.out.println( "Person " + (i+1) + " name :: " +
"" + person[i].getName() + ", Age :: " + person[i].getAge());
}
/*
Sort method of the Arrays class sorts the given array.
Signature of the sort method is,
static void sort(Object[] object, Comparator comparator)
IMPORTANT: All methods defined by Arrays class are static. Arrays class
serve as a utility class.
*/
/* Sorting array on the basis of person age by passing AgeComparator */
Arrays.sort(person, new AgeComparator());
System.out.println("\n\nOrder of person after sorting by person age is");
for(int i=0; i < person.length; i++){
System.out.println( "Person " + (i+1) + " name :: " +
"" + person[i].getName() + ", Age :: " + person[i].getAge());
}
/* Sorting array on the basis of person Name by passing NameComparator */
Arrays.sort(person, new NameComparator());
System.out.println("\n\nOrder of person after sorting by person name is");
for(int i=0; i < person.length; i++){
System.out.println( "Person " + (i+1) + " name :: " +
"" + person[i].getName() + ", Age :: " + person[i].getAge());
}
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author sowjanya.g
*/
import java.util.Arrays;
import java.util.Comparator;
/*
Java Comparator example.
This Java Comparator example describes how java.util.Comparator
interface is implemented to compare Java user defined classe's objects.
These Java Comparator is passed to Collection's sorting
method ( for example Collections.sort method)to perform sorting of Java user defined classe's objects.
*/
/*
java.util.Comparator interface declares two methods,
1) public int compare(Object object1, Object object2) and
2) boolean equals(Object object)
*/
/*
We will compare objects of the Person class using custom comparators
on the basis of person age and name.
*/
class Person{
private int age;
private String name;
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
}
/*
User defined java comaprator.
To create custom java comparator Implement Comparator interface and
define compare method.
The below given comparator compares persons on the basis of their age.
*/
class AgeComparator implements Comparator{
public int compare(Object emp1, Object emp2){
//parameter are of type Object, so we have to downcast it to Person objects
int emp1Age = ( (Person) emp1).getAge();
int emp2Age = ( (Person) emp2).getAge();
if( emp1Age > emp2Age ){
return 1;
}
else if( emp1Age < emp2Age ){
return -1;
}
else{
return 0;
}
}
}
/* The below given comparator compares persons on the basis of their name. */
class NameComparator implements Comparator{
public int compare(Object emp1, Object emp2){
//parameter are of type Object, so we have to downcast it to Person objects
String emp1Name = ( (Person) emp1 ).getName();
String emp2Name = ( (Person) emp2 ).getName();
//uses compareTo method of String class to compare names of the person
return emp1Name.compareTo(emp2Name);
}
}
/*
This Java comparator example compares persons on the basis of
their age and name and sort it in that order.
*/
public class JavaComparatorExample{
public static void main(String args[]){
/* Person array which will hold persons */
Person person[] = new Person[2];
//set different attributes of the individual person.
person[0] = new Person();
person[0].setAge(40);
person[0].setName("Joe");
person[1] = new Person();
person[1].setAge(20);
person[1].setName("Mark");
System.out.println("Order of person before sorting is");
//print array as is.
for(int i=0; i < person.length; i++){
System.out.println( "Person " + (i+1) + " name :: " +
"" + person[i].getName() + ", Age :: " + person[i].getAge());
}
/*
Sort method of the Arrays class sorts the given array.
Signature of the sort method is,
static void sort(Object[] object, Comparator comparator)
IMPORTANT: All methods defined by Arrays class are static. Arrays class
serve as a utility class.
*/
/* Sorting array on the basis of person age by passing AgeComparator */
Arrays.sort(person, new AgeComparator());
System.out.println("\n\nOrder of person after sorting by person age is");
for(int i=0; i < person.length; i++){
System.out.println( "Person " + (i+1) + " name :: " +
"" + person[i].getName() + ", Age :: " + person[i].getAge());
}
/* Sorting array on the basis of person Name by passing NameComparator */
Arrays.sort(person, new NameComparator());
System.out.println("\n\nOrder of person after sorting by person name is");
for(int i=0; i < person.length; i++){
System.out.println( "Person " + (i+1) + " name :: " +
"" + person[i].getName() + ", Age :: " + person[i].getAge());
}
}
}
No comments:
Post a Comment