I've tried this a couple ways, the first is have a class that implements comparator at the bottom of the following code. When I try to pass the comparat in sortListByLastName, I get a constructor not found error and I am not sure why
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());
}
}
And then when I tried doing it by creating an anonymous inner class, I also get a constructor TreeMap cannot find symbol error. Any thoughts?
inner class method:
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;
}