简体   繁体   English

不同的集合用于不同的数据排序

[英]Different collections for different data sorting

I have a task to play with Java Collections framework. 我有一个与Java Collections框架一起玩的任务。 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). (这已完成,用户存储在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. 每个用户都是Person类的实例,其中描述了姓名,姓氏,出生日期,加入日期以及其他一些现在不重要的参数。 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 ?). 好的,首先,我的Person仅将数据存储为字符串(我应该将日期更改为Date吗?)。 I've started implementing sorting with "by name, surname, birthdate", cause that's what I get after calling sort on list with Strings. 我已经开始使用“by name,surname,birthdate”实现排序,因为这是我在使用Strings调用sort列表后得到的结果。 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 : 这是我的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> : 我将实现Comparator<Person> ,然后对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). NameSurnameBirthComparator将实现Comparator<Person>并通过首先比较他们的名字,然后他们的姓氏(如果他们的名字相等)然后他们的出生日期(如果他们的姓氏相等)来比较两个人。

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 :) 是的,我会将出生Date存储为Date - 或者最好将其存储为JodaTimeLocalDate ,因为这是一个更好的日期和时间操作库:)

so I should write multiple comprators on Person for each task ? 所以我应该为每个人在Person上编写多个编译器?

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. 您可以使用Bean比较器对各个属性进行排序。

Then you can use the Group Comparator to sort on multiple properties. 然后,您可以使用组比较器对多个属性进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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