开发者

Different collections for different data sorting

开发者 https://www.devze.com 2023-01-21 00:03 出处:网络
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 a

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 f开发者_JAVA技巧unctionality 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号