关于定制比较器

时间:2012-08-04 11:30:18

标签: java collections

我正在尝试开发一个应用程序,其中arraylist将按名字排序,如果两个人的名字相同,那么将根据他们的工资进行进一步的比较,因为比较必须是请告知如何制作自定义比较器..现在我的程序仪式是现在只做比较工资仪式的基础..!

  package saxbean;

class Emp //implements Comparable
{
      String name,job;
      int salary;
      public Emp(String n,String j,int sal)
      {
         name=n;
         job=j;
         salary=sal;
       }
      public void display()
      {
        System.out.println(name+"\t"+job+"\t"+salary);
       }
     public boolean equals(Object o)
      {

         Emp p=(Emp)o;
          return this.name.equals(p.name)&&this.job.equals(p.job) &&this.salary==p.salary;
       }
     public int hashCode()
       {
          return name.hashCode()+job.hashCode()+salary;
       }


      /* public int compareTo(Object o)
       {
          Emp e=(Emp)o;
          return this.name.compareTo(e.name);
           //return this.job.compareTo(e.job);
        //   return this.salary-e.salary;

        }*/
} 

MY Comprator是......

import java.util.Comparator;

    class SalaryComparator implements Comparator {
        public int compare(Object paramObject1, Object paramObject2) {
            Emp localEmp1 = (Emp) paramObject1;
            Emp localEmp2 = (Emp) paramObject2;
            return localEmp1.salary - localEmp2.salary;
        }
    }

我的主要课程是......

class EmpListDemo
{
      public static void main(String arg[])
      {
          List  list=new ArrayList ();
          list.add(new Emp("Ram","Trainer",34000));
          list.add(new Emp("Sachin","Programmer",24000));
          list.add(new Emp("Ram","Trainer",34000));
          list.add(new Emp("Priyanka","Manager",54000));
          list.add(1,new Emp("Ravi","Administrator",44000));
          list.add(new Emp("Anupam","Programmer",34000));
          list.add(new Emp("Priyanka","Manager",54000));
          list.add(new Emp("Sachin","Team Leader",54000));
          System.out.println("There are "+list.size()+" elements in the list.");


          //original list
          System.out.println("Content of list are : ");
          ListIterator itr1=list.listIterator();
          while(itr1.hasNext())
          {
            Emp e=(Emp)itr1.next();  
            e.display();
          }           



          System.out.println("*****************");

          System.out.println("Sort Object according to Salary");
          Collections.sort(list,new SalaryComparator());
          System.out.println("Content of list are : ");
           itr1=list.listIterator();
           while(itr1.hasNext())
           {
             Emp e=(Emp)itr1.next();  
             e.display();
           }
          }
}

但是请告知如何开发一个应用程序,其中arraylist将按名字排序,如果两个人的名字相同,那么将根据他们的工资进行进一步的比较.. !!

2 个答案:

答案 0 :(得分:1)

class SalaryComparator implements Comparator {
        public int compare(Object paramObject1, Object paramObject2) {
            Emp localEmp1 = (Emp) paramObject1;
            Emp localEmp2 = (Emp) paramObject2;
            int result = localEmp1.getName().compareTo(localEmp2.getName());
            if (result == 0) {
                result = localEmp1.salary - localEmp2.salary;
            }
            return result;
        }
    }

答案 1 :(得分:0)

我猜你是这样的......

public class SalaryComparator implements Comparator<Emp> {

  @Override
  public int compare(Emp o1, Emp o2) {
    int result = o1.name.compareTo(o2.name);
    return result != 0 ? result : o1.salary - o2.salary;
  }
}

class Emp {
  String name;
  int salary;
}