Java Iterable Size getSingleIfExist(Iterable iterable)

Here you can find the source of getSingleIfExist(Iterable iterable)

Description

get Single If Exist

License

Open Source License

Declaration

public static <T> T getSingleIfExist(Iterable<T> iterable) 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.util.Iterator;

public class Main {
    public static <T> T getSingleIfExist(Iterable<T> iterable) {
        if (iterable == null)
            return null;
        final Iterator<? extends T> iter = iterable.iterator();
        if (!iter.hasNext())
            return null;
        final T result = iter.next();
        if (iter.hasNext()) {
            throw new IllegalArgumentException("More than (expected) one element in " + iterable);
        }//from   w w  w.ja  v  a 2 s.  co m
        return result;
    }
}

Related

  1. countElements(Iterable pairs)
  2. getIterableSize(Iterable iterable)
  3. getSingle(Iterable it)
  4. getSingleElementOrNull(Iterable coll)
  5. getSingleOrNull(Iterable iterable)
  6. iterableSize(Iterable iterable)
  7. iterableSize(Iterable itrbl)
  8. iterableSizeEq(Iterable itrbl, int k)