关于比较条件

时间:2012-05-07 17:12:10

标签: java

我在下面开发了使用比较器..

这是我的pojo ..

  class Emp
{
      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;
       }  

} 

这是我用户定义的集合类..

  ArrayList list=new ArrayList();
          list.add(new Emp("Ram","Trainer",34000));
          list.add(new Emp("Sachin","Programmer",24000));
          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("Sachin","Team Leader",54000));
          System.out.println("There are "+list.size()+" elements in the list.");
          System.out.println("Content of list are : ");
         ListIterator itr=list.listIterator();
          System.out.println("Sort Object according to Salary");
         Collections.sort(list,new SalaryComparator());
         System.out.println("Content of list are : ");
          itr=list.listIterator();
          while(itr.hasNext())
          {
            Emp e=(Emp)itr.next();  
            e.display();
          }
      }

最后我的比较器类

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

现在它正在按工资递增顺序排序但请建议我如何在隔离区中实现逻辑,这样首先按工资分类,然后根据工作,即第一个工资,然后工作,请指教。

2 个答案:

答案 0 :(得分:1)

您可以在比较器功能中始终包含多个比较。在这种情况下:

class SalaryComparator
  implements Comparator
{
  public int compare(Object paramObject1, Object paramObject2)
  {
    Emp localEmp1 = (Emp)paramObject1;
    Emp localEmp2 = (Emp)paramObject2;
    int salaryDiff = localEmp1.salary - localEmp2.salary;
    if (salaryDiff != 0) {
      return salaryDiff;
    } else {
      if (localEmp1.job == null) {
        if (localEmp2.job == null) {
          return 0;
        } else {
          return 1;
        }
      } else {
        if (localEmp2.job == null) {
          return -1;
        } else {
          return localEmp1.job.compareTo(localEmp2.job);
        }
      }
    }
  }
}

这种空检查设置将Emp实例放在字母排名末尾没有作业值。

答案 1 :(得分:0)

您可以使用泛型使代码更安全:

import java.util.*;

class Emp
// as is

// use Comparator <Emp> 
class SalaryComparator implements Comparator <Emp>
{
    // 'Emp' as param type, no casting or isinstanceof inside
    public int compare (Emp emp1, Emp emp2)
    {
        int diff = emp1.salary - emp2.salary;
        if (diff != 0) return diff; 
        else return (emp1.job.compareTo (emp2.job));
    }
}

public class EmpTest 
{
    public static void main (final String args[])
    {
        // how to use collections generically should be known by now:
        ArrayList <Emp> list = new ArrayList <Emp> ();
        list.add (new Emp ("Ram","Trainer", 34000));
        // ...

        // position 3 - slightly more complicated here ...
        ListIterator <Emp> itr = list.listIterator ();
        // ...
        while (itr.hasNext ())
        {
                // but simpler here:
            Emp e= itr.next ();  
            e.display ();
        }
    // here you could use the simplified for-loop:
    for (Emp e : list)
    {
        e.display ();
    }
    }
}