Skip to content

Commit

Permalink
Added lastOrNull and deprecated last also in IteratorExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Apr 9, 2024
1 parent 21b629c commit e5f5f02
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public static <T> T lastOrNull(Iterable<T> iterable) {
return null;
return sortedSet.last();
} else {
return IteratorExtensions.last(iterable.iterator());
return IteratorExtensions.lastOrNull(iterable.iterator());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,24 @@ public static <T> Iterator<T> tail(final Iterator<T> iterator) {
* @param iterator
* the iterator. May not be <code>null</code>.
* @return the last element in the iterator or <code>null</code>.
* @deprecated Use {@link #lastOrNull(Iterator<T>)} instead. To be aligned with
* {@link IterableExtensions#lastOrNull(Iterable)} (see the deprecation reason of
* {@link IterableExtensions#last(Iterable)}).
*/
@Deprecated
public static <T> T last(Iterator<T> iterator) {
return lastOrNull(iterator);
}

/**
* Returns the last element in the given iterator or <code>null</code> if empty.
*
* @param iterator
* the iterator. May not be <code>null</code>.
* @return the last element in the iterator or <code>null</code>.
* @since 2.35
*/
public static <T> T lastOrNull(Iterator<T> iterator) {
T result = null;
while(iterator.hasNext()) {
result = iterator.next();
Expand Down

0 comments on commit e5f5f02

Please sign in to comment.