Hash Card Sort

I am trying to get acquainted with Collections. I have a String, which is my key, email address and Person object (firstName, lastName, phone, email). I read in the chapter of Java collections on Sun's web pages that if you have a HashMap and you want it to be sorted, you could use TreeMap. How does this view work? Is it based on the compareTo () method that you have in your Person class? I tried the compareTo () method in the Person class to sort by lastName. But it does not work properly and wondered if I have the right idea or not. getSortedListByLastName at the bottom of this code, where I am trying to convert to TreeMap. Also, if this is the right way to do this, or one of the right ways to do this, as I then sort firstName,since comparing compareTo () by lastName.

import java.util.*;

public class OrganizeThis 
{
    /** 
    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()
    {
        Map<String, Person> sorted = new TreeMap<String, Person>(staff);
        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);
        }
    }
    }

:

public class Person implements Comparable
{
    String firstName;
    String lastName;
    String telephone;
    String email;

    public Person()
    {
       firstName = "";
       lastName = "";
       telephone = "";
       email = "";
    }

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

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

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public String getTelephone()
    {
        return telephone;
    }

    public void setTelephone(String telephone)
    {
        this.telephone = telephone;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public int compareTo(Object o)
    {
        String s1 = this.lastName + this.firstName;
        String s2 = ((Person) o).lastName + ((Person) o).firstName;
        return s1.compareTo(s2);
    }

    public boolean equals(Object otherObject)
    {
        // a quick test to see if the objects are identical
        if (this == otherObject) {
            return true;
        }

        // must return false if the explicit parameter is null
        if (otherObject == null) {
            return false;
        }

        if (!(otherObject instanceof Person)) {
            return false;
        }

        Person other = (Person) otherObject;
        return firstName.equals(other.firstName) && lastName.equals(other.lastName) &&
            telephone.equals(other.telephone) && email.equals(other.email);
    }

    public int hashCode() 
    {
        return this.email.toLowerCase().hashCode();
    }

    public String toString()
    {
        return getClass().getName() + "[firstName = " + firstName + '\n'
                                    + "lastName = " + lastName + '\n'
                                    + "telephone = " + telephone + '\n'
                                    + "email = " + email + "]";
    }


}
+1
2

.

:

  • Map<K,V> - K key V value
  • TreeMap<K,V> - SortedMap<K,V>, ,

, a TreeMap<String,Person> , Person / .

SortedSet<Person> List<Person>, , Person implements Comparable<Person> Comparator<Person>.

API


, :

import java.util.*;

public class Example {
    static String lastName(String fullName) {
        return fullName.substring(fullName.indexOf(' ') + 1);
    }
    public static void main(String[] args) {
        Map<String,String> map = new TreeMap<String,String>();
        map.put("001", "John Doe");
        map.put("666", "Anti Christ");
        map.put("007", "James Bond");

        System.out.println(map);
        // "{001=John Doe, 007=James Bond, 666=Anti Christ}"
        // Entries are sorted by keys!

        // Now let make a last name Comparator...
        Comparator<String> lastNameComparator = new Comparator<String>() {
            @Override public int compare(String fullName1, String fullName2) {
                return lastName(fullName1).compareTo(lastName(fullName2));
            }
        };

        // Now let put all names in a SortedSet...
        SortedSet<String> namesByLastName =
            new TreeSet<String>(lastNameComparator);
        namesByLastName.addAll(map.values());

        System.out.println(namesByLastName);
        // "[James Bond, Anti Christ, John Doe]"
        // Names sorted by last names!

        // Now let use a List instead...
        List<String> namesList = new ArrayList<String>();
        namesList.addAll(map.values());
        System.out.println(namesList);
        // "[John Doe, James Bond, Anti Christ]"
        // These aren't sorted yet...

        Collections.sort(namesList);
        System.out.println(namesList);
        // "[Anti Christ, James Bond, John Doe]"
        // Sorted by natural ordering!

        // Now let sort by string lengths...
        Collections.sort(namesList, new Comparator<String>() {
            @Override public int compare(String s1, String s2) {
                return Integer.valueOf(s1.length()).compareTo(s2.length());
            }
        });
        System.out.println(namesList);
        // "[John Doe, James Bond, Anti Christ]"
        // SUCCESS!!!
    }
}
+8

, a SortedMap , .

, LinkedHashMap Map , . A LinkedHashMap , a List.

- / , . List<Entry<K, V>>, , , Collections#sort() Compatator<Entry<K, V>> , , LinkedHashMap ( a HashMap ).

( ):

// Prepare.
Map<String, String> map = new HashMap<String, String>();
map.put("foo", "bar");
map.put("bar", "waa");
map.put("waa", "foo");
System.out.println(map); // My JVM shows {waa=foo, foo=bar, bar=waa}

// Get entries and sort them.
List<Entry<String, String>> entries = new ArrayList<Entry<String, String>>(map.entrySet());
Collections.sort(entries, new Comparator<Entry<String, String>>() {
    public int compare(Entry<String, String> e1, Entry<String, String> e2) {
        return e1.getValue().compareTo(e2.getValue());
    }
});

// Put entries back in an ordered map.
Map<String, String> orderedMap = new LinkedHashMap<String, String>();
for (Entry<String, String> entry : entries) {
    orderedMap.put(entry.getKey(), entry.getValue());
}

System.out.println(orderedMap); // {foo=bar, waa=foo, bar=waa}

, ;)

0

All Articles