在地图中存储带有日期的对象组
我有一组在某些日期发生的事件。每个事件都有一个日期字段。现在我想创建一个地图,其中对于每个日期(取自事件的所有日期),我将分配该日期发生的事件列表。所以在伪代码中:
public Map<Date, List<Event>> function(List<Event> list){
Date[]dates = new Date(list.len());
for(Object o: list)
add o.date to dates
for(int i=0; i<dates.length; i++){
create list of events with date=dates[i] (using some getDate())
add to map(dates[i], list)
}
}
这是正确的思维方式吗?如果是:如何创建具有特定日期的事件列表,然后将其添加到地图中?我只是从收藏开始。
编辑
所以我正在尝试使用 hisdrewness 的解决方案。最后一个问题是如何检索具有所需日期的事件。所以我正在我的地图上创建一个迭代器,但接下来该怎么办?在 python 中这很容易,但是如何在 Java 中“获取带有 date=date 的对象”?
private String getItems(Date date){
String ret = "";
// DatesSortedMap is my previously built map and it works properly
Iterator i = this.DatesSortedMap.entrySet().iterator();
while( i.hasNext() ){
//how I can get to the object while having iterator ?
if(object.date = date)
ret += object;
}
return ret;
}
I have a group of Events that occurred at some Dates. Each event has a Date field. Now I'd like to create a Map where for each Date (taken from all dates of events) I will assign List of Events that occurred on that date. So in pseudocode :
public Map<Date, List<Event>> function(List<Event> list){
Date[]dates = new Date(list.len());
for(Object o: list)
add o.date to dates
for(int i=0; i<dates.length; i++){
create list of events with date=dates[i] (using some getDate())
add to map(dates[i], list)
}
}
Is this a proper way of thinking? If yes: how can I create list of events with specific date and then add it to the map? I'm just starting with the collections.
EDIT
So I'm trying to use solution of hisdrewness. The last problem is how to retrieve events with the desired Date. So I'm creating an Iterator over my map but what next ? In python it is easy but how can I 'get objects with date=date' in Java ?
private String getItems(Date date){
String ret = "";
// DatesSortedMap is my previously built map and it works properly
Iterator i = this.DatesSortedMap.entrySet().iterator();
while( i.hasNext() ){
//how I can get to the object while having iterator ?
if(object.date = date)
ret += object;
}
return ret;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果其他人需要这样的东西,现在(Java8+)可以这样做,例如,像这样(Event property
created
is LocalDateTime)。例如 TreeMap(如果需要排序)。Collectors.toCollection(ArrayList::new))
可能会更改为Collectors.toList
(不保证列表类型)。也可以使用
Collectors.groupingBy(event -> event.getCreated().toLocalDate())
但您无法指定 Map 的类型If someone else needs something like this, now (Java8+) it can be done, for example, like this (Event property
created
is LocalDateTime). TreeMap for examle (if sorting needed).Collectors.toCollection(ArrayList::new))
may be changed toCollectors.toList
(List type not guaranteed).Will also work
Collectors.groupingBy(event -> event.getCreated().toLocalDate())
but you won't be able to specify the type of Map以下是我将如何编写此方法:
或在伪代码中:
一个重要的警告是将日期作为哈希键进行比较。应考虑时区、毫秒精度等因素。
编辑
用于迭代返回值:
Here's how I would code this method:
or in psuedo code:
One important caveat is comparing the dates as hash keys. Things like time zone, millisecond precision, etc. should be considered.
EDIT
For iterating over the return value:
你大部分时间都在那儿。您无需遍历日期,而是可以遍历事件,在遍历时将每个事件添加到正确的“存储桶”中。
You're mostly there. Instead of iterating through the dates, you can iterate through the events, adding each event to the proper "bucket" as you go through.