gpt4 book ai didi

java - 查找时间序列中值的变化

转载 作者:搜寻专家 更新时间:2023-11-01 03:34:28 26 4
gpt4 key购买 nike

我有一个 map ,以下列格式存储一个人的工资的时间序列数据

HashMap<Date,Double> salaryHistory;

变量 salaryHistory 可以有从公元 1 年到甚至 2100 年的数据。

我正在使用 subMap 从 hashmap 中过滤数据,但我在以下场景中面临挑战

考虑这样的人的薪水

Jan-01-1969, 100
Jan-01-1979, 200

当用户询问 Jan-1-1970 到 Jan-1-1972 之间的薪水时,subMap 返回“null”,但实际上它应该返回 100,因为,此人的薪水是 100 1969 年,直到 1979 年才改变。

有没有简单的方法来做到这一点?就像一个图书馆。

欢迎大家提出宝贵意见

最佳答案

我发现如果您使用 SortedMap 而不是 HashMap,您会得到预期的行为:

   Date j1969 = DateTimeUtils.convertStringToDate("1969-01-01");
Date j1974 = DateTimeUtils.convertStringToDate("1974-01-01");
Date j1979 = DateTimeUtils.convertStringToDate("1979-01-01");
Date j1989 = DateTimeUtils.convertStringToDate("1989-01-01");

TreeMap<Date, Double> treemap = new TreeMap<Date, Double>();
SortedMap<Date, Double> treemapincl = new TreeMap<Date, Double>();

// populating tree map
treemap.put(j1969, 100.0);
treemap.put(j1979, 200.0);
treemap.put(j1989, 300.0);

treemapincl=treemap.subMap(j1969,j1974);
System.out.println("Sub map values: "+treemapincl);

输出:

Sub map values: {Wed Jan 01 00:00:00 GMT-05:00 1969=100.0}

关于java - 查找时间序列中值的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35544947/

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