Skip to content

Commit

Permalink
Add a note about SequencedCollection.getFirst() / getLast() to `I…
Browse files Browse the repository at this point in the history
…terables.getFirst()` / `getLast()`.

RELNOTES=n/a
PiperOrigin-RevId: 733371274
  • Loading branch information
kluever authored and Google Java Core Libraries committed Mar 4, 2025
1 parent 5004483 commit 8a9497c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions android/guava/src/com/google/common/collect/Iterables.java
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,11 @@ public Iterator<T> iterator() {
*
* <p><b>{@code Stream} equivalent:</b> {@code stream.findFirst().orElse(defaultValue)}
*
* <p><b>Java 21+ users:</b> if {code iterable} is a {@code SequencedCollection} (e.g., any list),
* consider using {@code collection.getFirst()} instead. Note that if the collection is empty,
* {@code getFirst()} throws a {@code NoSuchElementException}, while this method returns the
* default value.
*
* @param defaultValue the default value to return if the iterable is empty
* @return the first element of {@code iterable} or the default value
* @since 7.0
Expand All @@ -839,6 +844,9 @@ public Iterator<T> iterator() {
*
* <p><b>{@code Stream} equivalent:</b> {@link Streams#findLast Streams.findLast(stream).get()}
*
* <p><b>Java 21+ users:</b> if {code iterable} is a {@code SequencedCollection} (e.g., any list),
* consider using {@code collection.getLast()} instead.
*
* @return the last element of {@code iterable}
* @throws NoSuchElementException if the iterable is empty
*/
Expand All @@ -863,6 +871,11 @@ public Iterator<T> iterator() {
*
* <p><b>{@code Stream} equivalent:</b> {@code Streams.findLast(stream).orElse(defaultValue)}
*
* <p><b>Java 21+ users:</b> if {code iterable} is a {@code SequencedCollection} (e.g., any list),
* consider using {@code collection.getLast()} instead. Note that if the collection is empty,
* {@code getLast()} throws a {@code NoSuchElementException}, while this method returns the
* default value.
*
* @param defaultValue the value to return if {@code iterable} is empty
* @return the last element of {@code iterable} or the default value
* @since 3.0
Expand Down
13 changes: 13 additions & 0 deletions guava/src/com/google/common/collect/Iterables.java
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,11 @@ public Spliterator<T> spliterator() {
*
* <p><b>{@code Stream} equivalent:</b> {@code stream.findFirst().orElse(defaultValue)}
*
* <p><b>Java 21+ users:</b> if {code iterable} is a {@code SequencedCollection} (e.g., any list),
* consider using {@code collection.getFirst()} instead. Note that if the collection is empty,
* {@code getFirst()} throws a {@code NoSuchElementException}, while this method returns the
* default value.
*
* @param defaultValue the default value to return if the iterable is empty
* @return the first element of {@code iterable} or the default value
* @since 7.0
Expand All @@ -829,6 +834,9 @@ public Spliterator<T> spliterator() {
*
* <p><b>{@code Stream} equivalent:</b> {@link Streams#findLast Streams.findLast(stream).get()}
*
* <p><b>Java 21+ users:</b> if {code iterable} is a {@code SequencedCollection} (e.g., any list),
* consider using {@code collection.getLast()} instead.
*
* @return the last element of {@code iterable}
* @throws NoSuchElementException if the iterable is empty
*/
Expand All @@ -853,6 +861,11 @@ public Spliterator<T> spliterator() {
*
* <p><b>{@code Stream} equivalent:</b> {@code Streams.findLast(stream).orElse(defaultValue)}
*
* <p><b>Java 21+ users:</b> if {code iterable} is a {@code SequencedCollection} (e.g., any list),
* consider using {@code collection.getLast()} instead. Note that if the collection is empty,
* {@code getLast()} throws a {@code NoSuchElementException}, while this method returns the
* default value.
*
* @param defaultValue the value to return if {@code iterable} is empty
* @return the last element of {@code iterable} or the default value
* @since 3.0
Expand Down

0 comments on commit 8a9497c

Please sign in to comment.