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

宿題によく似ている。どこを見ればいいのか、ヒントを教えてください。

次の 2 つの選択肢があります。

  • POJO クラスを拡張するComparable<Customer>
  • カスタム外部コンパレータを として定義しますComparator<Customer>

明示的な顧客が 2 人いるという 2 番目の選択肢を想定すると、次のようなメソッドを定義する必要があります。

@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
@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 に答える
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 に答える