Vale
Vale

Reputation: 1124

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?

Upvotes: 0

Views: 85

Answers (2)

Vale
Vale

Reputation: 1124

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

Upvotes: 0

Tom Van Rossom
Tom Van Rossom

Reputation: 1470

Make use of Guava's ListMultimap:

ListMultimap<String, String> people = ArrayListMultimap.create();
people.putAll("miao", new ArrayList("bau","cip","muu");

people.get("miao").size();//returns 3

Upvotes: 1

Related Questions