在地图中存储带有日期的对象组

发布于 2024-09-29 01:43:25 字数 1053 浏览 2 评论 0原文

我有一组在某些日期发生的事件。每个事件都有一个日期字段。现在我想创建一个地图,其中对于每个日期(取自事件的所有日期),我将分配该日期发生的事件列表。所以在伪代码中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东风软 2024-10-06 01:43:25

如果其他人需要这样的东西,现在(Java8+)可以这样做,例如,像这样(Event property created is LocalDateTime)。例如 TreeMap(如果需要排序)。 Collectors.toCollection(ArrayList::new)) 可能会更改为 Collectors.toList (不保证列表类型)。

List<Event> events ...

Map<LocalDate, List<Event>> = events.stream().collect(
            Collectors.groupingBy(
                event -> event.getCreated().toLocalDate(),
                TreeMap::new,
                Collectors.mapping(event -> event, Collectors.toCollection(ArrayList::new))
            )
        );

也可以使用 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 to Collectors.toList (List type not guaranteed).

List<Event> events ...

Map<LocalDate, List<Event>> = events.stream().collect(
            Collectors.groupingBy(
                event -> event.getCreated().toLocalDate(),
                TreeMap::new,
                Collectors.mapping(event -> event, Collectors.toCollection(ArrayList::new))
            )
        );

Will also work Collectors.groupingBy(event -> event.getCreated().toLocalDate()) but you won't be able to specify the type of Map

捶死心动 2024-10-06 01:43:25

以下是我将如何编写此方法:

public Map<Date, List<Event>> function(List<Event> list){
    Map<Date, List<Event>> sortedEvents = new HashMap<Date, List<Event>>();
    for(Event event : list) {
        Date eventDate = event.getDate();
        if(!sortedEvent.containsKey(eventDate)) {
            sortedEvent.put(eventDate, new ArrayList<Event>());
        }
        sortedEvent.get(eventDate).add(event);
    }
}

或在伪代码中:

Loop through events
   Get event date
   If Map does not contain member for event date
      Create new member for event date
   End if
   Add event for given event date
End Loop

一个重要的警告是将日期作为哈希键进行比较。应考虑时区、毫秒精度等因素。

编辑

用于迭代返回值:

Map<Date, List<Event>> map = // call sort function
for(Map.Entry<Date, List<Event>> entry : map.entrySet()) {
    Date date = entry.getKey();
    List<Event> events = entry.getValue();
}

Here's how I would code this method:

public Map<Date, List<Event>> function(List<Event> list){
    Map<Date, List<Event>> sortedEvents = new HashMap<Date, List<Event>>();
    for(Event event : list) {
        Date eventDate = event.getDate();
        if(!sortedEvent.containsKey(eventDate)) {
            sortedEvent.put(eventDate, new ArrayList<Event>());
        }
        sortedEvent.get(eventDate).add(event);
    }
}

or in psuedo code:

Loop through events
   Get event date
   If Map does not contain member for event date
      Create new member for event date
   End if
   Add event for given event date
End Loop

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:

Map<Date, List<Event>> map = // call sort function
for(Map.Entry<Date, List<Event>> entry : map.entrySet()) {
    Date date = entry.getKey();
    List<Event> events = entry.getValue();
}
━╋う一瞬間旳綻放 2024-10-06 01:43:25

你大部分时间都在那儿。您无需遍历日期,而是可以遍历事件,在遍历时将每个事件添加到正确的“存储桶”中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文