Java Collection Element Get get(Collection p_oCol, T p_oObj)

Here you can find the source of get(Collection p_oCol, T p_oObj)

Description

This method provides a way to obtain an object from a collection, given an equal object (logical equals).

License

Apache License

Parameter

Parameter Description
p_oCol a parameter
p_oObj a parameter

Declaration

public static <T> T get(Collection<T> p_oCol, T p_oObj) 

Method Source Code

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

import java.util.Collection;

import java.util.Iterator;

import java.util.List;
import java.util.Map;

public class Main {
    /**//from  w  w  w .  j  a  v a2 s  .  c  om
     * This method provides a way to obtain an object from a collection, given
     * an equal object (logical equals). In the collection and set interfaces
     * (even list) the assumption is that if you have an equal object why would
     * you need the one out of the collection. Because logically equal objects
     * can be physically different objects we provide this method to return out
     * of a collection the actual object in the collection ( for partial object
     * checking and untrusted object arguements that reference an existing item
     * in a collection ).
     * 
     * @param p_oCol
     * @param p_oObj
     * @return
     */
    public static <T> T get(Collection<T> p_oCol, T p_oObj) {
        if (p_oObj == null)
            return null;
        if (isEmpty(p_oCol))
            return null;
        if (p_oCol instanceof List)
            return get(p_oCol, ((List<T>) p_oCol).indexOf(p_oObj));
        T oObj = null;
        T oTempObj = null;

        for (Iterator<T> oIter = p_oCol.iterator(); oIter.hasNext();) {
            oTempObj = oIter.next();
            if (p_oObj.equals(oTempObj)) {
                oObj = oTempObj;
                break;
            }
        }

        return oObj;
    }

    /**
     * Mostly provided for sets, Please keep in mind sets don't guarantee any
     * specific order. And it could be that for any one instance of a set this
     * method may return 2 different objects for the same index (dependent on
     * the set implementation) This method is useful for getting a single item
     * out of a collection when you don't know what type of collection you are
     * dealing with.
     * 
     * @param p_oCol
     * @param p_iIndex
     * @return
     */
    public static <T> T get(Collection<T> p_oCol, int p_iIndex) {
        if (isEmpty(p_oCol))
            return null;
        if (p_iIndex < 0 || p_iIndex >= p_oCol.size())
            return null;
        if (p_oCol instanceof List)
            return ((List<T>) p_oCol).get(p_iIndex);

        T oObj = null;
        T oTempObj = null;
        int i = 0;

        for (Iterator<T> oIter = p_oCol.iterator(); oIter.hasNext();) {
            oTempObj = oIter.next();
            if (i++ == p_iIndex) {
                oObj = oTempObj;
                break;
            }
        }

        return oObj;
    }

    public static boolean isEmpty(Iterable<?> i) {
        if (i instanceof Collection)
            return ((Collection<?>) i).isEmpty();
        return i == null || !i.iterator().hasNext();
    }

    public static boolean isEmpty(Map<?, ?> p_oCol) {
        return p_oCol == null || p_oCol.isEmpty();
    }

    public static int size(Collection<?> col) {
        return col == null ? 0 : col.size();
    }
}

Related

  1. get(Collection coll, int index)
  2. get(Collection p_collection, int p_index)
  3. get(Collection arr, int idx)
  4. get(Collection collection, int index)
  5. get(Collection v, int i)
  6. get(final Collection list, final int pos)
  7. get(final Collection collection, final int index)
  8. getAddedItems(Collection oldValues, Collection newValues)