TreeMap of GregorianCalendars

114 Views Asked by At

I have created a TreeMap of <GregorianCalendar, Integer> to store the dates in which GPS leap seconds were introduced:

leapSecondsDict = new TreeMap<GregorianCalendar, Integer>();
GregorianCalendar calendar =
    new GregorianCalendar(TimeZone.getTimeZone("UTC"));

calendar.set(1981, GregorianCalendar.JUNE, 30, 23, 59, 59);
leapSecondsDict.put(calendar, 1);
calendar.set(1982, GregorianCalendar.JUNE, 30, 23, 59, 59);
leapSecondsDict.put(calendar, 2);
calendar.set(1983, GregorianCalendar.JUNE, 30, 23, 59, 59);
leapSecondsDict.put(calendar, 3);

The problem is that each time I call put, the TreeMap contains only the last inserted element, and the size stays 1.

Why is this happening? My guess is that when using the Calendar as a key, the key is the class instance id and not its value, so when inserting the second element into the TreeMap I would be updating an existing entry with the same key, even if that key is now the same class with a different value.

Is my assumption right, or is there any other reason? Is there any farcier way of achieving this other than creating a new Calendar for each entry?

2

There are 2 best solutions below

0
On BEST ANSWER

The set method does not create a new Calendar, it is still the same but with a different value. Therefore, when you add it to the Map it simply updates the value as its the same key, you are always referencing the same object. You need to create a new instance for each of the keys.

0
On

As Balduz commented:

The set method does not create a new Calendar, it is still the same but with a different value. Therefore, when you add it to the Map it simply updates the value as its the same key, you are always referencing the same object. You need to create a new instance for each of the keys.

So, you should create a new one each time, something like:

GregorianCalendar calendar1 = new GregorianCalendar(1981, GregorianCalendar.JUNE, 30, 23, 59, 59);
calendar1.setTimeZone(TimeZone.getTimeZone("UTC"));
leapSecondsDict.put(calendar1, 1);
GregorianCalendar calendar2 = new GregorianCalendar(1982, GregorianCalendar.JUNE, 30, 23, 59, 59);
calendar2.setTimeZone(TimeZone.getTimeZone("UTC"));
leapSecondsDict.put(calendar2, 2);
GregorianCalendar calendar3 = new GregorianCalendar(1983, GregorianCalendar.JUNE, 30, 23, 59, 59);
calendar3.setTimeZone(TimeZone.getTimeZone("UTC"));
leapSecondsDict.put(calendar3, 3);