关于比较器的发展

时间:2012-05-09 16:54:01

标签: java

我有一个查询我开发了一个pojo ..

public class Customer {
int Age;
public Customer(int age, String surname, String forename) {
    super();
    Age = age;
    Surname = surname;
    Forename = forename;
}


String Surname,Forename;

public int  getAge() {
    // TODO Auto-generated method stub
    return Age;
}

public String getSurname() {
    // TODO Auto-generated method stub
    return Surname;
}


public String getForename() {
    // TODO Auto-generated method stub
    return Surname;
}



 public void display()
 {
  // System.out.println(Forename+"\t"+Surname+"\t"+Age);
     System.out.println(Age+"\t"+Forename+"\t"+Surname);
  }

}

这是我的收藏班..

    class testCustomerComparator
{

    public static void main(String... a)
    {

            Customer customerFirst = new Customer(46,"Alabama", "Christonson");
        Customer customerSecond = new Customer(21, "Anna", "Sobek");
        Customer customerThird = new Customer(27, "Rafael", "Sobek");

        List<Customer> list = new ArrayList<Customer>();
        list.add(customerThird);
        list.add(customerSecond);
        list.add(customerFirst);
}
}

请告诉我如何为这个班级制作计算器,我想制作比较器,以便按年龄排序客户列表,按姓氏排序。之后你想按名字排序。请告知我在比较器中有嵌套条件

好吧,好像有点像......

   public class CustomerComparator implements Comparator<Customer> {

  @Override
  public int compare(Customer c1, Customer c2) {

    if (c1.getAge() == c2.getAge()) {
      if (c1.getSurname().compareTo(c2.getSurname()) == 0) {
          return c1.getForename().compareTo(c2.getForename()) {
      } else {
          return c1.getSurname().compareTo(c2.getSurname());
      }
    } else if (c1.getAge() > b2.getAge()) {
        return -1;
    } else {
        return 1;
    }
  }

但它不起作用请告知

4 个答案:

答案 0 :(得分:1)

看起来很像家庭作业。我可以给你一些关于在哪里看的提示。

您有两种选择:

  • 使POJO类扩展Comparable<Customer>
  • 将自定义外部比较器定义为Comparator<Customer>

假设第二个选择,你有两个明确的客户,你必须定义一个类似于这个的方法:

@Override
public int compare(Customer c1, Customer c2)
{
  // this method should return 0 if c1.equals(c2), 
  // should instead return 1 if c1 should come first than c2 and -1 otherwise
}

答案 1 :(得分:0)

public class CustomerComparator implements Comparator<Customer> {

public int compare(Customer c1, Customer c2) {
 .... here you have c1 and c2. compare returns -1 if c1 should go before c2,
 0 if they are found to be equal, and 1 if c2 should go before c1.
 You add the logic to compare c1 and c2 fields as you stated and return the result.
 }

 }

然后使用Collections.sort使用此比较器对该列表进行排序。

答案 2 :(得分:0)

您可以提供以下代码的帮助。

import java.util.*;

class Customer {

    private int age;
    private String name;
    private String forename;

    public Customer(int age, String surname, String forename) {
        super();
        this.age = age;
        this.name = surname;
        this.forename = forename;
    }

    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;
    }

    public void setForename(String forename) {
        this.forename = forename;
    }

    public String getForename() {
        return forename;
    }
}

class AgeComparator implements Comparator {

    public int compare(Object emp1, Object emp2) {

        int emp1Age = ((Customer) emp1).getAge();
        int emp2Age = ((Customer) emp2).getAge();

        if (emp1Age > emp2Age)
            return 1;
        else if (emp1Age < emp2Age)
            return -1;
        else
            return 0;
    }

}

/*
 * The below given comparator compares employees 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 Employee
        // objects

        int emp1Age = ((Customer) emp1).getAge();
        int emp2Age = ((Customer) emp2).getAge();

        if (emp1Age > emp2Age) {
            return 1;
        } else if (emp1Age < emp2Age) {
            String emp1Name = ((Customer) emp1).getName();
            String emp2Name = ((Customer) emp2).getName();

            // uses compareTo method of String class to compare names of the
            // employee
            return emp1Name.compareTo(emp2Name);
        } else {
            return 0;
        }
    }

}

class CustomerComparator implements Comparator {

    public int compare(Object emp1, Object emp2) {

        // parameter are of type Object, so we have to downcast it to Employee
        // objects

        String emp1Name = ((Customer) emp1).getName();
        String emp2Name = ((Customer) emp2).getName();

        // uses compareTo method of String class to compare names of the
        // employee
        return emp1Name.compareTo(emp2Name);

    }

}

public class JavaComparatorExample {

    public static void main(String args[]) {

        // Employee array which will hold employees
        Customer employee[] = new Customer[3];

        // set different attributes of the individual employee.
        employee[0] = new Customer(46, "Alabama", "Christonson");
        employee[1] = new Customer(21, "Anna", "Sobek");
        employee[2] = new Customer(27, "Rafael", "Sobek");

        System.out.println("Order of employee before sorting is");
        // print array as is.
        for (int i = 0; i < employee.length; i++) {
            System.out.println("Employee " + (i + 1) + " name :: "
                    + employee[i].getName() + ", Age :: "
                    + employee[i].getAge());
        }

        Arrays.sort(employee, new AgeComparator());
        System.out
                .println("\n\nOrder of employee after sorting by employee age is");

        for (int i = 0; i < employee.length; i++) {
            System.out.println("Employee " + (i + 1) + " name :: "
                    + employee[i].getName() + ", Age :: "
                    + employee[i].getAge());
        }

        // Sorting array on the basis of employee Name by passing NameComparator
        Arrays.sort(employee, new NameComparator());

        System.out
                .println("\n\nOrder of employee after sorting by employee name is");
        for (int i = 0; i < employee.length; i++) {
            System.out.println("Employee " + (i + 1) + " name :: "
                    + employee[i].getName() + ", Age :: "
                    + employee[i].getAge());
        }

    }

}

希望这会对你有所帮助。

修改

查看CustomerComparator班。

答案 3 :(得分:0)

@Override
public int compare(Customer c1, Customer c2) {
  int r = Integer.valueOf(c1.getAge()).compareTo(c2.getAge());
  if (r != 0) return r;
  r = c1.getSurname().compareTo(c2.getSurname());
  if (r != 0) return r;
  return c1.getForename().compareTo(c2.getForename());
}