Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

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 are distinct.

Aren’t those two supposed to return the same values?

1 Answer

0 votes
by (13.1k points)

Given that the collection returned by TreeMap’s values() method has a size as follows-

public int size() {

    return TreeMap.this.size();

}

I would 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 is no way they can return two different values for the same map with the same contents.

Want to learn Java? Check out the core Java certification from Intellipaat.

Related questions

0 votes
2 answers
0 votes
1 answer
asked Feb 12, 2021 in Java by Jake (7k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...