get Single Element Of Iterable - Java java.util

Java examples for java.util:Iterable Element

Description

get Single Element Of Iterable

Demo Code


//package com.java2s;

import java.util.Iterator;

public class Main {
    public static <T> T getSingleElementOf(Iterable<T> iterable) {
        Iterator<T> iterator = iterable.iterator();
        T first = iterator.next();/*  w  w  w . ja  v  a 2  s.  co  m*/
        if (iterator.hasNext())
            throw new IllegalArgumentException(
                    "Expected only a single element in " + iterable);
        return first;
    }
}

Related Tutorials