public int single() {
        if (iterator.hasNext()) {
            int singleCandidate = iterator.nextInt();
            if (iterator.hasNext()) {
                throw new IllegalStateException("IntStream contains more than one element");
            } else {
                return singleCandidate;
            }
        } else {
            throw new NoSuchElementException("IntStream contains no element");
        }
    }
public int single ( ) { if ( iterator . hasNext ( ) ) { int singleCandidate = iterator . nextInt ( ) ; if ( iterator . hasNext ( ) ) { throw new IllegalStateException ( "IntStream contains more than one element" ) ; } else { return singleCandidate ; } } else { throw new NoSuchElementException ( "IntStream contains no element" ) ; } }
Returns the single element of stream.
If stream is empty, throws {@code NoSuchElementException}.
If stream contains more than one element, throws {@code IllegalStateException}.

<p>This is a short-circuiting terminal operation.

<p>Example:
<pre>
stream: []
result: NoSuchElementException

stream: [1]
result: 1

stream: [1, 2, 3]
result: IllegalStateException
</pre>

@return single element of stream
@throws NoSuchElementException if stream is empty
@throws IllegalStateException if stream contains more than one element
@since 1.1.3