Java 在时间序列中查找值的变化
我有一张地图,以以下格式存储一个人工资的时间序列数据Java 在时间序列中查找值的变化,java,hashmap,time-series,treemap,Java,Hashmap,Time Series,Treemap,我有一张地图,以以下格式存储一个人工资的时间序列数据 HashMap<Date,Double> salaryHistory; 当用户询问1970年1月1日至1972年1月1日之间的工资时,subMap返回“null”,但实际上它应该返回100,因为此人的工资在1969年为100,直到1979年才发生变化 有没有一个简单的方法可以做到这一点?就像图书馆一样 请提供您的宝贵建议我发现如果您使用SortedMap而不是HashMap,您将获得您期望的行为: Date j1969 =
HashMap<Date,Double> salaryHistory;
当用户询问1970年1月1日至1972年1月1日之间的工资时,subMap
返回“null”,但实际上它应该返回100,因为此人的工资在1969年为100,直到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);
如果您不必使用<代码> HashMap <代码>,请考虑用<代码> TeeMePa/COD>替代它。然后,您就可以使用来获取相关的数据。
您的逻辑不能只测试给定的日期是否在两个键之间,并提供两个值中的较低值吗? 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}