Java Collection Element Get getAny(final Collection collection)

Here you can find the source of getAny(final Collection collection)

Description

Returns an arbitrary element from the given collection.

License

Apache License

Parameter

Parameter Description
collection to retrieve the element from

Return

an arbitrary element or null if the collection is empty

Declaration

public static <T> T getAny(final Collection<T> collection) 

Method Source Code

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

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**/*from www  .jav  a2s. c o m*/
     * Returns an arbitrary element from the given collection.
     * 
     * @param collection
     *        to retrieve the element from
     * @return an arbitrary element or <tt>null</tt> if the collection is empty
     */
    public static <T> T getAny(final Collection<T> collection) {
        final Iterator<T> iterator = collection.iterator();
        if (iterator.hasNext()) {
            return iterator.next();
        }

        return null;
    }
}

Related

  1. getAll(Map map, Collection indices)
  2. getAllDoubleValues( Map>> doubleCollectionWithValuesToFetch)
  3. getAllTopicsAsString(final Collection topics)
  4. getAncestors(Collection> classes)
  5. getAnElement(Collection coll)
  6. getAnyFrom(Collection collection)
  7. getArbitraryMember(Collection s)
  8. getArray(Collection dnaCol)
  9. getArrayFromCollection( Collection collection)