简体   繁体   中英

HashMap size() of multiple values that have the same key

I created a hashMap of flights, the keys are the city names, which in this case one key could have multiple flight values. public HashMap<String,List> flights;

I added 20 pairs of key-value data into the map, but the size() method only returns 10 which is the number of keys. But when I print the information using a nested for loop, I could get all the flight info. I am wondering what is the output of the size() method of HashMap(). How would I get the size of the key-value pairs 20 in this case?

You can use Map#values and then sum all the sizes.

int size = map.values().stream().mapToInt(List::size).sum();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM