Java Utililty Methods Collection Element Get

List of utility methods to do Collection Element Get

Description

The list of methods to do Collection Element Get are organized into topic(s).

Method

MapgetCardinalityMap(final Collection col)
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 .
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 {
...
MapgetCardinalityMap(final Collection coll)
get Cardinality Map
Map count = new HashMap<T, Integer>();
for (T item : coll) {
    Integer c = (Integer) (count.get(item));
    if (c == null) {
        count.put(item, INTEGER_ONE);
    } else {
        count.put(item, new Integer(c.intValue() + 1));
return count;
StringgetCaseInsensitive(Collection data, String needle)
get Case Insensitive
for (String curString : data)
    if (curString.equalsIgnoreCase(needle))
        return curString;
return null;
ClassgetClassFromCollection(Collection collection)
Finds a class type that is in the containing collection, will always return something (failsafe to Object.class)
Class<?> c = Object.class;
if (collection != null) {
    if (!collection.isEmpty()) {
        c = collection.iterator().next().getClass();
    } else {
        c = Object.class;
return c;
StringgetClosest(final String pattern, Collection targets)
get Closest
int score = -1;
String best = null;
for (String target : targets) {
    int thisScore = matchLength(pattern, target);
    if (thisScore > score) {
        score = thisScore;
        best = target;
return best;
CollectiongetCollection(final Iterable iterable)
get Collection
final Enumeration<Object[]> e = new Enumeration<Object[]>() {
    final Iterator<Path> inner = iterable.iterator();
    public boolean hasMoreElements() {
        return inner.hasNext();
    @Override
    public Object[] nextElement() {
        return new Object[] { inner.next() };
...
intgetCollection(final String iText, final int iStartPosition, final Collection iCollection)
get Collection
final StringBuilder buffer = new StringBuilder();
int openPos = iText.indexOf(COLLECTION_BEGIN, iStartPosition);
if (openPos == -1)
    return -1;
int currentPos, deep;
for (currentPos = openPos + 1, deep = 1; deep > 0; currentPos++) {
    if (currentPos >= iText.length()) {
        return -1;
...
CollectiongetCollection(Object entity)
get Collection
List<Object> rs = new ArrayList<Object>();
rs.add(entity);
return rs;
CollectiongetCollection(T... items)
Returns a Collection with the given items.
List<T> list = new ArrayList<T>();
for (T item : items) {
    list.add(item);
return list;
Class>getCollectionClass(TT tt)
This function helps avoid compile warnings by just having the warning here.
return (Class<Collection<TT>>) Collection.class.asSubclass(Collection.class);