gpt4 book ai didi

java - 在 Java 中使用日期作为 TreeMultimap 的键时记录丢失

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:21 26 4
gpt4 key购买 nike

我正在编写一个调度程序,其中创建的事件标记为日期。因此,通过选择日期,将显示标记到该日期的所有事件。我尝试使用 google 的 TreeMultimap 执行此操作,但我意识到并非所有与所选日期相关的记录都会返回。我想知道使用日期作为 TreeMultimap 的键是否存在任何已知问题,或者纯粹是我的代码问题。

TreeMultimap<Date, EventInstance> dateToEventMultimap = TreeMultimap.create();

// Build multimap.
Calendar cal1 = GregorianCalendar.getInstance();
Calendar cal2 = GregorianCalendar.getInstance();
cal1.clear(); // Clears the values of all the time fields.
cal2.clear();
for (EventInstance eventInstance : myEventList) {
cal1.setTime(eventInstance.getTimeDate());
cal2.set(cal1.get(Calendar.YEAR),
cal1.get(Calendar.MONTH),
cal1.get(Calendar.DAY_OF_MONTH));
dateToEventMultimap.put(cal2.getTime(), eventInstance);
}

最佳答案

如果使用可变对象作为键并且它被修改,则将不再找到该记录,因为散列将不同。它将保留在旧的哈希“bin”中,但 HashMap 实现将在基于新哈希键的“bin”中搜索它,并且它不会在那里(除非纯粹是偶然的,对于良好的哈希代码实现来说这是相当小的)。

关于java - 在 Java 中使用日期作为 TreeMultimap 的键时记录丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5671725/

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