user1621988
user1621988

Reputation: 4335

Get size/length of a value in a HashMap

Map<Integer, String> map = new HashMap<Integer, String>();

How do I get the size/length of matching value's at the String?

example:

1 , Red 2 , Red 3 , Blue 4 , Blue 5 , Red

Size of the String of RED = 3

Upvotes: 2

Views: 14376

Answers (2)

kennytm
kennytm

Reputation: 523284

Use .values() to get a collection containing all the values of the hash map, and then use Collections.frequency() to count the number of objects in the collection.

return Collections.frequency(map.values(), "red");

Upvotes: 10

RNJ
RNJ

Reputation: 15552

Or do you mean the count?

If so call

map.values()

to get a list of the values.

Then you can iterate through it and count how many time RED appears

Upvotes: 0

Related Questions