Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Map's size() method return? [closed]

Tags:

java

map

size

I have a map declared as follows -

Map<Date, Long[]> myMap = new TreeMap<Date, Long[]>();

I put some key-value pairs in that map, check the size as follows -

myMap.size(); //returns 29

myMap.values().size(); //returns 31

All the dates (keys) are distinct.

Aren't those two supposed to return same values?

like image 906
Bhesh Gurung Avatar asked Sep 15 '11 02:09

Bhesh Gurung


1 Answers

Given that the collection returned by TreeMap's values() method (in JDK 6, at least) has a size as follows:

public int size() {
    return TreeMap.this.size();
}

I'd say you have something adding new entries to the map between your two size() calls. To be clear, map.values().size() delegates to map.size(). Therefore there's no way they can return two different values for the same map with the same contents.

like image 185
Ryan Stewart Avatar answered Sep 18 '22 05:09

Ryan Stewart