日期的 Java 哈希映射搜索键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7120052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java hashmap search keys for a date
提问by user902201
I have a hashmap: Map dateEvent = new HashMap(); where key is a date and time and value is a string. I fill collection with data where date is in format dd.MM.yyyy HH:mm. How I can get all keys with date based on this format: dd.MM.yyyy?
我有一个哈希图: Map dateEvent = new HashMap(); 其中键是日期和时间,值是字符串。我用日期格式为 dd.MM.yyyy HH:mm 的数据填充集合。如何根据以下格式获取所有带日期的键:dd.MM.yyyy?
回答by Bohemian
This code will do the trick:
这段代码可以解决问题:
public static void findEvents(Map<Date, Event> dateEvents, Date targetDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
String target = dateFormat.format(targetDate);
for (Map.Entry<Date, Event> entry : dateEvents.entrySet()) {
if (dateFormat.format(entry.getKey()).equals(target)) {
System.out.println("Event " + entry.getValue() + " is on the specified date");
}
}
}
The important thing here is that all dates are converted to a String with format "dd.MM.yyyy" before comparing, so any differences in hour/minute/second still match if the day is the same.
这里重要的是所有日期在比较之前都转换为格式为“dd.MM.yyyy”的字符串,因此如果日期相同,小时/分钟/秒的任何差异仍然匹配。
This code also demonstrates the best way (IMHO) to iterate over a map.
此代码还演示了迭代地图的最佳方式(恕我直言)。
回答by Chris
Not sure whether I get you right. However, you get the set of keys from a map using map.keySet()
. If you want to find all different dates fill all dates into a set. If you want to reduce the accuracy to days, one solution would be to convert the date to the desired format and add those strings to a set. Duplicates will be removed automatically.
不确定我是否理解你的意思。但是,您可以使用map.keySet()
. 如果要查找所有不同的日期,请将所有日期填入一个集合中。如果您想将精度降低到天,一种解决方案是将日期转换为所需的格式并将这些字符串添加到集合中。重复项将被自动删除。
E.g.:
例如:
Map<Date, String> yourMap = [..];
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
Set<String> differentDates = new HashSet<String>();
for (Date date: yourMap.keySet()) {
differentDates.add(simpleDateFormat.format(date));
}
回答by musiKk
You have (at least) two options:
您(至少)有两个选择:
You could write your own Date class which supplies appropriate implementations of hashCode()
and equals()
. (If you do this, it is not recommended that you base that class on another class that already defines these methods (e.g. java.util.Date
).)
你可以写你自己的日期类提供适当的实现hashCode()
和equals()
。(如果您这样做,不建议您将该类基于已经定义了这些方法的另一个类(例如java.util.Date
)。)
The brute force alternative is to scan all keys whether they fit your criteria.
蛮力替代方案是扫描所有键是否符合您的标准。
回答by oliholz
There is no difference between between the Date 01.01.2011
(dd.MM.yyyy)and the Date 01.01.2011 00:00
(dd.MM.yyyy HH:mm).
日期01.01.2011
(dd.MM.yyyy)和日期01.01.2011 00:00
( dd.MM.yyyy HH:mm)之间没有区别。
Date
holds a long
which has hours and minutes. Even for public Date(int year, int month, int date)
Date
持有一个long
有小时和分钟的。即使是public Date(int year, int month, int date)