HashMap>
我制作一个图表,将我的电子邮件可视化。我希望能够收到某一天的电子邮件。
这是一种不好的存储方式吗?
HashMap<DateTime, ArrayList<Email>>
还是将日期转换为字符串然后使用 HashMap
注意,日期添加时不带小时、分钟和秒,所以就像 例如,2010 年 6 月 7 日
。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DateTime
已经正确定义了equals
和hashcode
方法,因此使用它们作为HashMap
中的键是完全可以的。首先将它们转换为字符串并没有什么好处。但是,我建议,如果您只想存储年/月/日组件,那么您可能需要使用
LocalDate
而不是DateTime
。此外,您还可以考虑使用
TreeMap
而不是HashMap
,以便您的地图自动按日期排序。可能会很方便。DateTime
has properly definedequals
andhashcode
methods, so using those as the key in aHashMap
is perfectly OK. There's not much to be gained by converting them to strings first.I would suggest, however, that if you only want to store the year/month/day components, then you may want to use
LocalDate
instead ofDateTime
.Additionally, you could also consider using
TreeMap
rather thanHashMap
, so that your map is automatically sorted by date. Might be handy.