Java Utililty Methods Collection First

List of utility methods to do Collection First

Description

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

Method

TnullSafeFirstElement(Collection collection)
null Safe First Element
try {
    return collection.iterator().next();
} catch (Exception e) {
    return null;
ListremoveFirst(Collection collection, int numToRemove)
Removes and returns the first n items from the given collection.
int toRemove = Math.min(collection.size(), numToRemove);
List removed = new ArrayList(toRemove);
Iterator iter = collection.iterator();
for (int idx = 0; idx < toRemove; idx++) {
    removed.add(iter.next());
    iter.remove();
return removed;
...
CollectionremoveFirst(Collection c)
remove First
List<X> list;
if (c == null || c.size() == 0) {
    list = Collections.emptyList();
} else {
    list = new ArrayList<X>(c);
    list.remove(0);
return list;
...
Collectionsub(Collection coll, int first, int size)
sub
if (coll == null) {
    return null;
if (coll.size() < size && first == 0) {
    return coll;
} else {
    List sub = new ArrayList(size);
    int i = -1;
...
Collectionsubtract(Collection firstCollection, Collection secondCollection)
subtract
Collection collection = new ArrayList<>();
for (Object object : firstCollection) {
    if (!secondCollection.contains(object)) {
        collection.add(object);
return collection;
Collectionunion(Collection firstSet, Collection secondSet)
union
return null;