Multimap Java的Multimap - 分类时间表 [英] Multimap of Multimap Java - Categorize timetable
问题描述
我已经解析了一个 HTML 时间表并将每个主题加载到我的类对象中.所以我有我的科目的数组列表,其中包含姓名、老师、...、HOUR 和 DAY 的信息现在我想重建表格,所以我需要先对它进行分类.我认为最好的结构是这样的:
I have parsed a HTML timetable and loaded every Subject to my class object. So i have arrayList of my Subjects which has information of name, teacher,... ,HOUR, and DAY now i want to reconstruct the table and so I need to categorize it first. I think that best would be to have structure like this:
Monday: 1: Math, Czech, ...
2: History
...
Tuesday: 1: English, Geo
2...
...
在给定的时间内可以有多个主题,因此我尝试使用 Multimap 的 Multimap,但我无法在解析时声明它.
There can be mutiple subjects in given hour, therefore I tried to used Multimap of Multimap, but I am not able to declare it during my for parsing.
Multimap<String, Multimap<String, Subject>> timetable = HashMultimap.create();
...
for ...
timetable.put(subject.den, new HashMultimap<>(subject.hod, subject));
但它说 HashMultimap 有私人访问 com.google.common...我不知道如何正确地写这个.我也在考虑使用 Array,但我必须预先声明它,我想在一个循环中构建它.有任何想法吗?提前谢谢您
but it says that HashMultimap has private accesin com.google.common... I dont know how to correctly write this. I was also thinking about using Array, but I would have to pre-declare it and I would like to build this during one for cycle. Any ideas? Thank you in advance
推荐答案
看起来你想要的其实更多的是一个Map
,其中你想要的情况
It looks like what you want is actually more of a Map<String, Multimap<String, Subject>>
, in which case you want
Map<String, Multimap<String, Subject>> timetable = new HashMap<>();
for ...
Multimap<String, Subject> multimap = timetable.get(subject.den);
if (multimap == null) {
multimap = HashMultimap.create();
timetable.put(subject.den, multimap);
}
multimap.put(subject.hod, subject);
这篇关于Multimap Java的Multimap - 分类时间表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!