2010-05-15 4 views
1

두 가지 방법을 시도했습니다. 먼저 다음 코드의 맨 아래에 비교기를 구현하는 클래스가 있습니다. 내가 sortListByLastName에서 comparat을 통과 할 때, 나는 생성자를 찾을 수 없습니다 오류 및Java에서 비교기 구문 도움말 전달

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", "[email protected]"); 
     Person person2 = new Person("K", "W", "345-678-9999", "[email protected]"); 
     Person person3 = new Person("Phoebe", "Wang", "322-111-3333", "[email protected]"); 
     Person person4 = new Person("Nermal", "Johnson", "322-342-5555", "[email protected]"); 
     Person person5 = new Person("Apple", "Banana", "123-456-1111", "[email protected]"); 
     testObj.add(person1); 
     testObj.add(person2); 
     testObj.add(person3); 
     testObj.add(person4); 
     testObj.add(person5); 

     System.out.println(testObj.findByEmail("[email protected]")); 
     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()); 
    } 
} 

그리고 나는 익명의 내부 클래스를 생성하여 그 일을 시도 할 때, 나는 또한 생성자 트리 맵을받을 이유는 확실하지 않다 기호 오류를 찾을 수 없습니다. 이견있는 사람?

내부 클래스 방법 :

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

답변

4

비교기는 아닌 값 지도과 비교한다. 이다

, 당신의 Map 따라서 생성자 유형 Comparator<? super String>의 인수를 기대, String에서 Person에 매핑이 포함되어 있습니다.

성으로 정렬하려면 성을 키로 사용하면 String의 자연 순서가 나머지 부분을 처리합니다 (명시 적 비교 자 필요 없음).

2

@aioobe가 말합니다.

은 컴파일 오류가 유형 TreeMap<String, Person>(Comparator<Person>)의 생성자를 찾을 수 없다는 불평 (효과)입니다

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<K, V>(Comparator<V>) 
:

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

는하지만, 당신이 쓴 것은 서명이 존재하지 않는 생성자가 필요합니다