Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Main { private static final Integer ONE = new Integer(1); public static <T> Map<T, Integer> getCardinalityMap(final Collection<T> coll) { if (coll == null) { return null; } Map<T, Integer> result = new HashMap<T, Integer>(); Iterator<T> it = coll.iterator(); while (it.hasNext()) { T t = it.next(); Integer count = result.get(t); if (count == null) { result.put(t, ONE); } else { result.put(t, new Integer(count.intValue() + 1)); } } return result; } }