Object Comparison


Object Comparison

A tree set is a sorted collection. You insert elements into the collection in any order. When you iterate through the collection, the values are automatically presented in sorted order
How does the TReeSet know how you want the elements sorted? By default, the tree set assumes that you insert elements that implement the java.lang.Comparable interface. That interface defines a single method:
public interface Comparable<T>
{
   int compareTo(T other);
}
Several standard Java platform classes implement the Comparable interface. One example is the String class. Its compareTo method compares strings in dictionary order (sometimes called lexicographic order).
If you insert your own objects, you must define a sort order yourself by implementing the Comparable interface. There is no default implementation of compareTo in the Object class.
public class Emp implements Comparable<Emp>{
public int compareTo(Object o) {
        return name.compareTo(o.name);        
    }
}
However, using the Comparable interface for defining the sort order has obvious limitations. A given class can implement the interface only once. But what can you do if you need to sort a bunch of employee by name in one collection and by salary in another? Furthermore, what can you do if you need to sort objects of a class whose creator didn't bother to implement the Comparable interface?
In those situations, you tell the tree set to use a different comparison method, by passing a Comparator object into the TReeSet constructor. The java.util.Comparator interface declares a compare method with two explicit parameters:
public interface Comparator<T>
{
   int compare(T a, T b);
}

Just like the compareTo method, the compare method returns a negative integer if a comes before b, zero if they are identical, or a positive integer otherwise.
To sort Employee by their name, simply define a class that implements the Comparator interface:
public class ComparatorASE implements Comparator<Emp>{
    public int compare(Emp e1, Emp e2) {
       return(e1.name.compareTo(e2.name));           
    }
}
Actually, the Comparator<T> interface is declared to have two methods: compare and equals. Of course, every class has an equals method; thus, there seems little benefit in adding the method to the interface declaration.
Full Example of Using Comparable
public class Emp implements Comparable<Emp>{

    String name;
    String job;
    int sal;
    public Emp(){
       
    }
    public Emp(String n, String j, int s) {
        name = n;
        job = j;
        sal = s;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }

    public int compareTo(Emp o) {
              
        return name.compareTo(o.name);
           
            
    }
 
}
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        TreeSet list=new TreeSet();
        list.add(new Emp("naveen","SE",18000));
        list.add(new Emp("raj","SE",15000));
        list.add(new Emp("dinesh","Analyst",18000));
        Emp e=new Emp("raj","SE",15000);
        System.out.println(list.contains(e));
        Iterator i=list.iterator();
       while(i.hasNext()){
           Emp emp=(Emp)i.next();
           System.out.println(emp.name);
       }


    }
}

Full Example using Comparator:

public class Emp {

    String name;
    String job;
    int sal;
    public Emp(){
       
    }
    public Emp(String n, String j, int s) {
        name = n;
        job = j;
        sal = s;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }

}
public class ComparatorASE implements Comparator<Emp>{

    public int compare(Emp e1, Emp e2) {

       return(e1.name.compareTo(e2.name));
           
    }

}
public class ComparatorDSC implements Comparator<Emp>{

      public int compare(Emp e1, Emp e2) {

       return(e2.name.compareTo(e1.name));

    }

}
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        TreeSet list=new TreeSet(new ComparatorASE());
        list.add(new Emp("naveen","SE",18000));
        list.add(new Emp("raj","SE",15000));
        list.add(new Emp("dinesh","Analyst",18000));
        Emp e=new Emp("raj","SE",15000);
        System.out.println(list.contains(e));
        Iterator i=list.iterator();
       while(i.hasNext()){
           Emp emp=(Emp)i.next();
           System.out.println(emp.name);
       }


    }
}

No comments:

Post a Comment