Java Collection Element Get getCardinalityMap(final Collection col)

Here you can find the source of getCardinalityMap(final Collection col)

Description

Returns a Map mapping each unique element in the given Collection to an Integer representing the number of occurances of that element in the Collection .

License

Apache License

Declaration

public static Map getCardinalityMap(final Collection col) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**//from  w w  w. ja  v a2s  .  com
     * Returns a {@link Map} mapping each unique element in
     * the given {@link Collection} to an {@link Integer}
     * representing the number of occurances of that element
     * in the {@link Collection}.
     * An entry that maps to <tt>null</tt> indicates that the
     * element does not appear in the given {@link Collection}.
     */
    public static Map getCardinalityMap(final Collection col) {
        HashMap count = new HashMap();
        Iterator it = col.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            Integer c = (Integer) (count.get(obj));
            if (null == c) {
                count.put(obj, new Integer(1));
            } else {
                count.put(obj, new Integer(c.intValue() + 1));
            }
        }
        return count;
    }
}

Related

  1. getAt(Collection col, int index)
  2. getAuthzNameFromEntityName(String entityName, Collection authzProvidersNames)
  3. getByIndex(Collection availableTransitions, int index)
  4. getByIndex(Collection vals, int idx)
  5. getByMostFrequent(Collection collection)
  6. getCardinalityMap(final Collection coll)
  7. getCaseInsensitive(Collection data, String needle)
  8. getClassFromCollection(Collection collection)
  9. getClosest(final String pattern, Collection targets)

  10. HOME | Copyright © www.java2s.com 2016