优先级队列排序字符串

时间:2015-10-15 02:59:57

标签: java sorting priority-queue

我需要使用PriorityQueue对Person对象进行排序。

Person类:

    public class Person implements Comparable<Person>{
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName){
       this.firstName = firstName;
       this.lastName = lastName;
    }

    public String getFirstName() {return firstName;}

    public String getLastName() {return lastName;}

    public String toString(){
       return "Name: " + firstName + " " + lastName + "\n";
    }

    public int compareTo(Person p){
       String pname1 = firstName + lastName;
       String pname2 = p.firstName + p.lastName;
       return pname1.compareTo(pname2);
    }

主要代码:

    import java.util.PriorityQueue;

    public class priorityQ{
        public static void main(String[] args){
           PriorityQueue<Person> pers = new PriorityQueue<Person>();
           pers.add(new Person("Sam", "Smith"));
           pers.add(new Person("Charlie", "Black"));
           pers.add(new Person("Betty", "Brown"));
           pers.add(new Person("Jessica", "Stewart"));
           pers.add(new Person("John", "Friday"));
           pers.add(new Person("Frank", "Folcy"));
           System.out.print(pers);      
       }   
    }

我需要:

  1. 无需排序打印(已在主页中完成)
  2. 按姓氏排序
  3. 按名字排序,然后按姓氏排序
  4. 我不能更改Person类中的任何代码。

    我创建了新类:

        import java.util.Comparator;
        public class LastNameComparator implements Comparator<Person>{
           public int compareTo(Person a, Person b){
             Person p1 = (Person)a;
             Person p2 = (Person)b;
             if(p1.getLastName().equals(p2.getLastName()))
             {
                return p1.getFirstName().compareTo(p2.getFirstName());
             }
             return p1.getLastName().compareTo(p2.getLastName());
           }
        }
    

    它有一个编译错误:

    LastNameComparator.java:2: error: LastNameComparator is not abstract and does not override abstract method compare(Person,Person) in Comparator
    

2 个答案:

答案 0 :(得分:0)

首先: 请删除

implements Comparable<Person>

 public int compareTo(Person p){
       String pname1 = firstName + lastName;
       String pname2 = p.firstName + p.lastName;
       return pname1.compareTo(pname2);
    }
来自Person类的

,因为你没有大声做出任何改变。

第二: 比较器类只有比较(T ob1,T2 ob2)方法,没有公共int比较(Person a,Person b)。 所以请创建比较(T ob1,T2 ob2)并在其上面放置@Override注释,以确保您做的一切正确。

毕竟你的比较器课必须

@Override
public int compare(Person a, Person b){
//..your code
}

其他代码似乎很好,从快速查看......

答案 1 :(得分:0)

首先,您必须从comparable interface及其所有实现中删除person class: 试着这样做:

import java.util.Comparator;
    public class LastNameComparator implements Comparator<Person>{    
    @Override
    public int compare(Person o1, Person o2) {

        return o1.lastname.compareTo(o2.lastname);
    }
    }

main method中执行以下操作以按姓氏排序:

Collections.sort(pers,new LastNameComparator());
    Iterator<Person> it = pers.iterator();
    while(it.hasNext()){
        System.out.println(it.next());
       }