简体   繁体   English

排序依据,更好-哈希图,树图,自定义实现

[英]Sorting by, which is better - hashmap, treemap, custom implementation

I have an ArrayList of Subjects and they have parameter Date , I want to group them into sorted array (maybe not array, but still sorted structure) of Day objects, so every Day will have parameter Date and object will contain only subjects with this date. 我有一个ArrayList of Subjects并且它们具有参数Date ,我想将它们分组为Day对象的排序数组(也许不是数组,但仍然是排序的结构),因此每个Day都有参数Date并且对象将只包含具有该日期的主题。 So the thing I wanna do is somehow group them by date and then get them. 所以我想做的就是以某种方式按日期对它们进行分组,然后将其获取。 I saw implementations of grouping by using HashMap, but then I have grouped structure but not sorted, so after that I should convert this to ArrayList for example. 我看到了使用HashMap进行分组的实现,但是后来我对结构进行了分组,但是没有进行排序,因此在此之后,我应该将其转换为ArrayList。 Or maybe I should use TreeMap, which will do the same but give me back sorted structure, or maybe best way is simply write my own sorter which will get ArrayList<Subject> and return ArrayList<Day> . 或也许我应该使用TreeMap,它会执行相同的操作,但是会返回排序后的结构,或者最好的方法是简单地编写自己的排序器,该排序器将获取ArrayList<Subject>并返回ArrayList<Day> Also I can use LinkedHashMap which will work too 我也可以使用LinkedHashMap

So now I have no idea what is better and what should I choose? 所以现在我不知道哪个更好,我应该选择什么? Important thing is that most likely I will not put new values or delete values from structure, I will only get them. 重要的是,很可能不会放置新值或从结构中删除值,而只会得到它们。

UPD: If I use map then Date will be key and Day object will be value. UPD:如果我使用地图,则“ Date将是关键,“ Day对象将是值。

By saying "get them" I meant iterate through them. 说“得到它们”是指遍历它们。

All this I'm doing in order to fill my UI elements with this info so most likely I will not search something in my structure later 我为了用此信息填充UI元素而做的所有事情,因此很可能以后我将不会在结构中搜索某些内容

Here's what I think you are asking for, but hopefully my answer can help even if it's not exactly it: 这是我认为您要的内容,但希望我的回答可以帮助您,即使并非完全如此:

  1. fast lookup using a Day as the key 以“ Day为键进行快速查找
  2. the result of that lookup should be sorted (ie multiple times of the same day are ordered) 查找的结果应排序(即订购同一天的多次)
  3. the possibility to see all subjects sorted by their Day 可以查看所有主题按其Day排序的可能性

Here's one option. 这是一个选择。 Use a Map that associates a Day to a sorted list of Subjects , so Map<Day, List<Subject>> . 使用Map ,一个相关联Day至排序列表Subjects ,所以Map<Day, List<Subject>> Since you don't need to add to it, you can build your mapping at the start and then sort it before you do any lookups. 由于不需要添加映射,因此可以在开始时构建映射,然后在进行任何查找之前对其进行排序。 Here's an outline: 这是一个大纲:

Map<Day, List<Subject>> buildMap(List<Subject> subjects) {
    Map<Day, List<Subject>> map = new HashMap<Day, List<Subject>>();
    // create a list of subjects for each day
    for (Subject subject : subjects) {
        if (!map.containsKey(subject.getDate().getDay())) {
            map.put(subject.getDate().getDay(), new ArrayList<Subject>());
        }
        map.get(subject.getDate().getDay()).add(subject);
    }

    // go through and sort everything now that you have grouped them
    for (Day day : map.keySet()) {
        Collections.sort(map.get(day));
    }

    return map;
}

If you also need to be able to 'get' every entry sorted throughout the map, you could maintain a sorted list of days. 如果还需要“获取”整个地图中排序的每个条目,则可以维护排序的日期列表。 Like so: 像这样:

List<Day> buildSortedDaysList(Map<Day, List<Subject>> map) {
    List<Day> sortedDays = new ArrayList<Day>(map.keySet());
    // again, many ways to sort, but I assume Day implements Comparable
    Collections.sort(sortedDays);
    return sortedDays;
}

You could then wrap it in a class, of which I recommend you create a better name: 然后,您可以将其包装在一个类中,我建议您创建一个更好的名称:

class SortedMapThing {
    Map<Day, List<Subject>> map;
    List<Day> orderedDays;

    SortedMapThing(List<Subject> subjects) {
        map = buildMap(subjects);
        orderedDays = buildSortedDaysList(map);
    }

    List<Subject> getSubject(Day day) {
        return map.get(day);
    }

    List<Subject> getAllSubjects() {
        List<Subject> subjects = new ArrayList<Subject>();
        for (Day day : orderedDays) {
            subjects.addAll(map.get(day));
        }
        return subjects;
    }

}

This implementation puts the work up front and gives you efficient lookup speed. 此实现使工作提前进行,并为您提供了高效的查找速度。 If I misunderstood your question slightly, you should be able to adjust it accordingly. 如果我对您的问题稍有误解,那么您应该可以相应地进行调整。 If I misunderstood your question entirely...I will be sad. 如果我完全误解了你的问题...我会很难过的。 Cheers! 干杯!

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

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