简体   繁体   English

使用Date作为密钥Java的Treemap

[英]Treemap using Date as the Key Java

What would be the correct method for being able to search a key of a TreeMap<Date, String> such as NavigableMap<Date, String> ? 能够搜索TreeMap<Date, String>的键的正确方法是什么TreeMap<Date, String>例如NavigableMap<Date, String> In which I wish to check by a specific month and output all those with that month? 我希望在特定月份检查并输出那个月的所有人?

For example if it looks like below, how would I be able to search for all those with the key February? 例如,如果它如下所示,我将如何能够搜索所有关键二月份的人?

Date date1 = dateformat.parse("8 January 2013");

If using a NavigableMap implementation, you can call subMap to get a view of the map between two keys: 如果使用NavigableMap实现,则可以调用subMap来获取两个键之间的映射视图:

DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy");

//using TreeMap for example
NavigableMap<Date, String> stringsByDate = new TreeMap<Date, String>();

//populate map
Date jan15th = dateFormat.parse("15 January 2013");
Date feb12th = dateFormat.parse("12 February 2013");
Date feb24th = dateFormat.parse("24 February 2013");
Date march18th = dateFormat.parse("18 March 2013");
stringsByDate.put(jan15th, "foo");
stringsByDate.put(feb12th, "bar");
stringsByDate.put(feb24th, "baz");
stringsByDate.put(march18th, "qux");

//define "from" and "to" keys
Date feb1st = dateFormat.parse("1 February 2013");
Date march1st = dateFormat.parse("1 March 2013");

//get the specified view
NavigableMap<Date, String> febStringsByDate = stringsByDate.subMap(
        feb1st,
        true,     //include Feb 1st
        march1st,
        false     //don't include March 1st
);

//prints {Tue Feb 12 00:00:00 EST 2013=bar, Sun Feb 24 00:00:00 EST 2013=baz}
System.out.println(febStringsByDate);

As demonstrated the "from" and "to" keys used in the call to subMap do not themselves need to be contained in the map. 如所示,调用subMap使用的“from”和“to”键本身不需要包含在地图中。

NOTE: This will only work for the dates of a specific February, eg February 2013. If you need entries for all February dates, you will need to iterate by year and consolidate the results. 注意:这仅适用于特定 2月的日期,例如2013年2月。如果您需要所有 2月日期的条目,则需要按年份进行迭代并合并结果。

  • First of all, Date is mutable and using mutable objects as a key in Map, is not recommended. 首先,不推荐Date是可变的,并且使用可变对象作为Map中的键。

  • but to answer your question: you need to iterate over keys in your map and check each one against the given criteria. 但要回答您的问题:您需要迭代地图中的键并根据给定的条件检查每个键。 you can wrap Date into Calendar class to make day, month, year and ... comparison easier. 您可以将Date包装到Calendar类中,以使日,月,年和...比较更容易。

Just having the same issue, took some time to figure out why it's not working. 只是有同样的问题,花了一些时间来弄清楚它为什么不起作用。

It simply compares the hash of the individual Date objects which is different even though the dates are equal. 它只是比较各个Date对象的哈希值,即使日期相同,它们也是不同的。

I am trying to use the following data structure 我试图使用以下数据结构

SortedMap<Date,List<String>>

but it doesn't allow to add multiple members to that list, as map.get(date) always returns null even if there is a same date there already. 但它不允许向该列表添加多个成员,因为即使已存在相同的日期,map.get(date)也始终返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM