Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/org/apache/commons/collections4/IterableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,22 @@ public static <E> E find(final Iterable<E> iterable, final Predicate<? super E>
return IteratorUtils.find(emptyIteratorIfNull(iterable), predicate);
}

/**
* Returns an {@link Optional} containing the first element in the
* {@code iterable}, or an empty {@code Optional} if the iterable is
* empty or null.
* <p>
* A {@code null} or empty iterator returns an empty {@code Optional}.
* </p>
*
* @param <E> the element type
* @param iterable the iterable to search, may be null
* @return an {@code Optional} containing the first element of the iterable or an empty {@code Optional} if the iterable is empty or null
*/
public static <E> Optional<E> any(final Iterable<E> iterable) {
return Optional.ofNullable(IteratorUtils.find(emptyIteratorIfNull(iterable), x -> true));
}

/**
* Shortcut for {@code get(iterator, 0)}.
* <p>
Expand Down