Vale Vale - 1 month ago 8
Java Question

How to get the size of the values in a Map?

I have a collection which I load at the beginning of a program and I give for granted that the String array that I use as value will have always the same length, for all the entries, plus that it will at least contain 1 value.

From these entries, I need to get the size of the array I've used. As of now, I'm doing it like so:

Map<String, String[]> people = new HashTable<>();
people.put("miao", new String[]{"bau","cip","muu"});
people.values().iterator().next().length; //returns 3


Is there a better way to do it?

Answer

According to @Eran (in the comments) I have already answered my own question in the question, so, to get the size of the Value (if it is an array) do:

Map<String, String[]> people = new HashTable<>();
people.put("miao", new String[]{"bau","cip","muu"});
people.values().iterator().next().length; //returns 3