简体   繁体   中英

Different collections for different data sorting

I have a task to play with Java Collections framework. I need to obtain a users list from a database, and store it in a collection. (This is finished and users are stored in a HashSet). Each user is an instance of Person class described with name, surname, birth date, join date, and some other parameters which are not important now. Next I need to store the list in different collections (there's nowhere stated how many) providing functionality to sort them by:
- name only
- name, surname, birthdate
- join date

Ok, so to start with, my Person stores data as Strings only (should I change dates to Date ?). I've started implementing sorting with "by name, surname, birthdate", cause that's what I get after calling sort on list with Strings. Am I right ?

public List createListByName(Set set){
    List ret = new ArrayList<String>();
    String data = "";

    for(Object p: set){
        data = p + "\n";
        ret.add(data);
    }
    Collections.sort(ret);
    return ret;
}

But what with the rest ? Here's my Person :

class Person {

    private String firstname;
    private String lastname;
    )..)


    Person(String name, String surname, (..)){
        firstname = name;
        lastname = surname;
        (..)
    }

    @Override
    public String toString(){
        return firstname + " " + lastname + " " + (..);
    }
}

I wouldn't convert everything to strings to start with. I would implement Comparator<Person> and then sort a List<Person> :

public List<Person> createListByName(Set<Person> set){
    List<Person> ret = new ArrayList<Person>(set);
    Collections.sort(ret, new NameSurnameBirthComparator());
    return ret;
}

The NameSurnameBirthComparator would implement Comparator<Person> and compare two people by first comparing their first names, then their surnames (if their first names are equal) then their birth dates (if their surnames are equal).

Something like this:

public int compare(Person p1, Person p2) {
    // TODO: Consider null checks, and what to do :)
    int firstNameResult = p1.getFirstName().compareTo(p2.getFirstName());
    if (firstNameResult != 0) {
        return firstNameResult;
    }
    int surnameResult = p1.getSurname().compareTo(p2.getSurname());
    if (surnameResult != 0) {
        return surnameResult;
    }
    return p1.getBirthDate().compareTo(p2.getBirthDate());
}

And yes, I would store the date of birth as a Date - or preferably as a LocalDate from JodaTime , as that's a much nicer library for date and time manipulation :)

so I should write multiple comprators on Person for each task ?

Given this is a homework task, then I would say that is the way you would start to learn about Comparators.

For interest sake only you can do this by creating a couple of resuable Comparators.

You can use the Bean Comparator to sort on individual properties.

Then you can use the Group Comparator to sort on multiple properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM