2

我有一个查询我开发了一个 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

4 回答 4

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
}
于 2012-05-09T17:01:50.990 回答
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 班级。

于 2012-05-09T17:03:43.470 回答
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());
}
于 2012-05-09T19:37:08.590 回答
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 使用此比较器对该列表进行排序。

于 2012-05-09T17:02:19.357 回答