gpt4 book ai didi

java - 有谁知道将根据时间间隔处理数据的库?

转载 作者:行者123 更新时间:2023-11-29 03:54:59 25 4
gpt4 key购买 nike

有人知道可以根据时间间隔操作/组织数据的库吗?

例如:

  • 我有一组按日期/时间映射到对象的数据[可以是任何东西,不相关数据的列表、字符串、数字等]
  • 我希望 Collection 足够通用,以便它可以处理间隔搜索、组织、合并、交集、差异和间隔更改 [即收集将重新组织 15 分钟的间隔,变为 1 天的间隔]

我正在考虑编写其中一个,但我不想重新发明轮子。

此外,最后一个规定是,我希望它使用 Java。

例如:

You have the following data:

  • 1/1/11 1:01 - "Bob entered the room"
  • 1/1/11 12:01 - "Jerry entered the room"
  • 1/1/11 1:31 - "Sally entered the room"
  • 1/1/11 1:51 - "Jorge entered the room"
  • 1/1/11 2:01 - "Dilbert entered the room"
  • 1/2/11 1:01 - "Bob entered the room"
  • 1/3/11 12:01 - "Jerry entered the room"
  • 1/2/11 1:31 - "Sally entered the room"
  • 1/2/11 1:51 - "Jorge entered the room"

All of the enteries stated here [as a 2 value datum] would go into the collection [i.e.]: add(Date, object) However on initialization the interval would be set. [I.e. 15minutes] So if the interval was 15 minutes [and queried for a specific time. It would produce:

  • 1/1/11 12:00-12:15
    • "Jerry entered the room"
  • 1/1/11 1:00-1:15
    - "Bob entered the room"
  • 1/1/11 1:15-1:30
  • 1/1/11 1:30-1:45

    • 1/1/11 1:31 - "Sally entered the room" ....

    If you tried to query for 1/1/11 12:09, you'd get the results for 1/1/11 12:00-12:15. Yes I realize there are edge cases, but this is an example.

最佳答案

这是一个 TreeMap 包装器的草图,它基本上完成了您的示例所显示的内容:

public class CalendarMap<V> {

private TreeMap<Calendar, V> map = new TreeMap<Calendar, V>();

public void put(Calendar d, V v){
map.put(d, v);
}

public void query(Calendar d, int intervalUnit, int intervalValue){
DateFormat df = new SimpleDateFormat("MMM dd, yyyy");
DateFormat tf = new SimpleDateFormat("HH:mm");

// snap closest prior unit
d.set(intervalUnit, (d.get(intervalUnit) / intervalValue)* intervalValue);
Calendar next = new GregorianCalendar();
next.setTime(d.getTime());
next.add(intervalUnit, intervalValue);

Calendar lastHit = null; // last hit

while(d.before(map.lastKey())){
SortedMap<Calendar, V> hits = map.subMap(d, true, next, false);
if(!hits.isEmpty()){
if(lastHit != null){
System.out.println(df.format(lastHit.getTime()) + " " + tf.format(lastHit.getTime()) + " - " +
df.format(d.getTime()) + " " + tf.format(d.getTime()) + ": N/A");
lastHit = null;
}
System.out.println(df.format(d.getTime()) + " " + tf.format(d.getTime()) + "-" + tf.format(next.getTime()) + ":");
for(Entry<Calendar, V> entry : hits.entrySet()){
System.out.println(" " + tf.format(entry.getKey().getTime()) + " - " + entry.getValue());
}
}else if(lastHit == null){
lastHit = new GregorianCalendar();
lastHit.setTime(d.getTime());
}
d.add(intervalUnit, intervalValue);
next.add(intervalUnit, intervalValue);
}
}

public static void main(String[] args){
CalendarMap<String> map = new CalendarMap<String>();
map.put(new GregorianCalendar(2011, 1, 1, 13, 1), "Bob entered the room");
map.put(new GregorianCalendar(2011, 1, 1, 12, 1), "Jerry entered the room");
map.put(new GregorianCalendar(2011, 1, 1, 13, 31), "Sally entered the room");
map.put(new GregorianCalendar(2011, 1, 1, 14, 1), "Dilbert entered the room");
map.put(new GregorianCalendar(2011, 1, 2, 13, 1), "Bob entered the room");
map.put(new GregorianCalendar(2011, 1, 3, 12, 1), "Jerry entered the room");
map.put(new GregorianCalendar(2011, 1, 2, 13, 31), "Sally entered the room");
map.put(new GregorianCalendar(2011, 1, 2, 13, 51), "Jorge entered the room");

map.query(new GregorianCalendar(2011, 1, 1, 12, 9), Calendar.MINUTE, 15);
}

}

这会产生:

Feb 01, 2011 12:00-12:15:  12:01 - Jerry entered the roomFeb 01, 2011 12:15 - Feb 01, 2011 13:00: N/AFeb 01, 2011 13:00-13:15:  13:01 - Bob entered the roomFeb 01, 2011 13:15 - Feb 01, 2011 13:30: N/AFeb 01, 2011 13:30-13:45:  13:31 - Sally entered the roomFeb 01, 2011 13:45 - Feb 01, 2011 14:00: N/AFeb 01, 2011 14:00-14:15:  14:01 - Dilbert entered the roomFeb 01, 2011 14:15 - Feb 02, 2011 13:00: N/AFeb 02, 2011 13:00-13:15:  13:01 - Bob entered the roomFeb 02, 2011 13:15 - Feb 02, 2011 13:30: N/AFeb 02, 2011 13:30-13:45:  13:31 - Sally entered the roomFeb 02, 2011 13:45-14:00:  13:51 - Jorge entered the roomFeb 02, 2011 14:00 - Feb 03, 2011 12:00: N/AFeb 03, 2011 12:00-12:15:  12:01 - Jerry entered the room

Suppose instead you execute this:

map.query(new GregorianCalendar(2011, 1, 1, 12, 9), Calendar.HOUR, 1);

那么输出是:

Feb 01, 2011 12:09-13:09:  13:01 - Bob entered the roomFeb 01, 2011 13:09-14:09:  13:31 - Sally entered the room  14:01 - Dilbert entered the roomFeb 01, 2011 14:09 - Feb 02, 2011 12:09: N/AFeb 02, 2011 12:09-13:09:  13:01 - Bob entered the roomFeb 02, 2011 13:09-14:09:  13:31 - Sally entered the room  13:51 - Jorge entered the roomFeb 02, 2011 14:09 - Feb 03, 2011 11:09: N/AFeb 03, 2011 11:09-12:09:  12:01 - Jerry entered the room

关于java - 有谁知道将根据时间间隔处理数据的库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6886169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com