I would use the Collections.frequency () method for HashMap values, for example.
int count = Collections.frequency(party.values(), 1); System.out.println(count); ===> 4
Or a general solution, generate a frequency map versus a number.
Map<Integer, Integer> counts = new HashMap<Integer, Integer>(); for (Integer c : party.values()) { int value = counts.get(c) == null ? 0 : counts.get(c); counts.put(c, value + 1); } System.out.println(counts); ==> {1=4, 2=1}
Adam
source share