在Java中传递比较器语法帮助

时间:2010-05-15 06:25:58

标签: java collections comparator

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

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

2 个答案:

答案 0 :(得分:4)

比较器应比较,而不是地图的

也就是说,您的Map包含从StringPerson的映射,因此构造函数会期望类型为Comparator<? super String>的参数。

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

答案 1 :(得分: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<String, Person>(Comparator<Person>)类型的构造函数。您打算使用的构造函数的真实类型签名是:

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

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

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