【问题标题】:Passing a comparator syntax help in Java在 Java 中传递比较器语法帮助
【发布时间】:2010-05-15 06:25:57
【问题描述】:

我已经尝试了几种方法,第一种是在以下代码的底部有一个实现比较器的类。当我尝试在 sortListByLastName 中传递比较时,我得到一个未找到构造函数的错误,我不知道为什么

import java.util.*;

public class OrganizeThis implements WhoDoneIt
{
    /** 
    Add a person to the organizer

    @param p A person object
    */
    public void add(Person p)
    {   
        staff.put(p.getEmail(), p);
        //System.out.println("Person " + p + "added");
    }

    /**
    * Remove a Person from the organizer.
    *
    * @param email The email of the person to be removed.
    */
    public void remove(String email)
    {
        staff.remove(email);
    }

    /**
    * Remove all contacts from the organizer.
    *
    */
    public void empty()
    {
        staff.clear();
    }

    /**
    * Find the person stored in the organizer with the email address.
    * Note, each person will have a unique email address.
    * 
    * @param email The person email address you are looking for.
    *
    */
    public Person findByEmail(String email)
    {
        Person aPerson = staff.get(email);
        return aPerson;
    }

    /**
    * Find all persons stored in the organizer with the same last name.
    * Note, there can be multiple persons with the same last name.
    * 
    * @param lastName The last name of the persons your are looking for.
    *
    */
    public Person[] find(String lastName)
    {
        ArrayList<Person> names = new ArrayList<Person>();

        for (Person s : staff.values())
        {
            if (s.getLastName() == lastName) {
                names.add(s);
            }
        }
        // Convert ArrayList back to Array
        Person nameArray[] = new Person[names.size()];
        names.toArray(nameArray);
        return nameArray;
    }

    /**
    * Return all the contact from the orgnizer in
    * an array sorted by last name.
    * 
    * @return An array of Person objects.
    *
    */
    public Person[] getSortedListByLastName()
    {
        PersonLastNameComparator comp = new PersonLastNameComparator();
        Map<String, Person> sorted = new TreeMap<String, Person>(comp);


        ArrayList<Person> sortedArrayList = new ArrayList<Person>();
        for (Person s: sorted.values()) {
            sortedArrayList.add(s);
        }
        Person sortedArray[] = new Person[sortedArrayList.size()];
        sortedArrayList.toArray(sortedArray);
        return sortedArray;
    }


    private Map<String, Person> staff = new HashMap<String, Person>();

    public static void main(String[] args)
    {
        OrganizeThis testObj = new OrganizeThis();
        Person person1 = new Person("J", "W", "111-222-3333", "JW@ucsd.edu");
        Person person2 = new Person("K", "W", "345-678-9999", "KW@ucsd.edu");
        Person person3 = new Person("Phoebe", "Wang", "322-111-3333", "phoebe@ucsd.edu");
        Person person4 = new Person("Nermal", "Johnson", "322-342-5555", "nermal@ucsd.edu");
        Person person5 = new Person("Apple", "Banana", "123-456-1111", "apple@ucsd.edu");
        testObj.add(person1);
        testObj.add(person2);
        testObj.add(person3);
        testObj.add(person4);
        testObj.add(person5);

        System.out.println(testObj.findByEmail("JW@ucsd.edu"));
        System.out.println("------------" + '\n');

        Person a[] = testObj.find("W");

        for (Person p : a)
        System.out.println(p);

        System.out.println("------------" + '\n');
        a = testObj.find("W");

        for (Person p : a)
        System.out.println(p);

        System.out.println("SORTED" + '\n');
        a = testObj.getSortedListByLastName();
        for (Person b : a) {
            System.out.println(b);
        }

        System.out.println(testObj.getAuthor());
    }
}

class PersonLastNameComparator implements Comparator<Person>
{
    public int compare(Person a, Person b)
    {
        return a.getLastName().compareTo(b.getLastName());
    }
}

然后当我尝试通过创建匿名内部类来执行此操作时,我还得到一个构造函数 TreeMap 找不到符号错误。有什么想法吗?

内部类方法:

public Person[] getSortedListByLastName()
    {
        //PersonLastNameComparator comp = new PersonLastNameComparator();
        Map<String, Person> sorted = new TreeMap<String, Person>(new Comparator<Person>()
        {
                public int compare(Person a, Person b)
    {
        return a.getLastName().compareTo(b.getLastName());
    }
    });


        ArrayList<Person> sortedArrayList = new ArrayList<Person>();
        for (Person s: sorted.values()) {
            sortedArrayList.add(s);
        }
        Person sortedArray[] = new Person[sortedArrayList.size()];
        sortedArrayList.toArray(sortedArray);
        return sortedArray;
    }

【问题讨论】:

    标签: java collections comparator


    【解决方案1】:

    比较器应该比较键,而不是地图的值。

    也就是说,您的Map 包含从String 到Person 的映射,因此构造函数将期望Comparator&lt;? super String&gt; 类型的参数。

    要按姓氏对它们进行排序,只需使用姓氏作为键,其余的将由字符串的自然排序处理(无需显式比较器)。

    【讨论】:

      【解决方案2】:

      正如@aioobe 所说。

      解释编译错误(例如)...

      Map<String, Person> sorted = 
          new TreeMap<String, Person>(new Comparator<Person>() {
              public int compare(Person a, Person b) {
                  return a.getLastName().compareTo(b.getLastName());
              }
          });
      

      编译错误(实际上)抱怨它找不到TreeMap&lt;String, Person&gt;(Comparator&lt;Person&gt;) 类型的构造函数。您打算使用的构造函数的真正类型签名是:

          TreeMap<K, V>(Comparator<K>)
      

      但是你写的需要一个不存在的带有签名的构造函数:

          TreeMap<K, V>(Comparator<V>)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-01
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 1970-01-01
        相关资源
        最近更新 更多