Skip to content

Commit 961370b

Browse files
klueverGoogle Java Core Libraries
authored andcommitted
Improve Iterables.getLast(Iterable) when the Iterable is a SortedSet.
RELNOTES=n/a PiperOrigin-RevId: 786266428
1 parent 9277137 commit 961370b

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

android/guava-tests/test/com/google/common/collect/IterablesTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ public void testGetLast_withDefault_not_empty_list() {
860860
public void testGetLast_emptySortedSet() {
861861
SortedSet<String> sortedSet = ImmutableSortedSet.of();
862862
assertThrows(NoSuchElementException.class, () -> Iterables.getLast(sortedSet));
863+
assertEquals("c", Iterables.getLast(sortedSet, "c"));
863864
}
864865

865866
public void testGetLast_iterable() {

android/guava/src/com/google/common/collect/Iterables.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Queue;
3737
import java.util.RandomAccess;
3838
import java.util.Set;
39+
import java.util.SortedSet;
3940
import java.util.stream.Stream;
4041
import org.jspecify.annotations.NonNull;
4142
import org.jspecify.annotations.Nullable;
@@ -859,6 +860,8 @@ public Iterator<T> iterator() {
859860
throw new NoSuchElementException();
860861
}
861862
return getLastInNonemptyList(list);
863+
} else if (iterable instanceof SortedSet) {
864+
return ((SortedSet<T>) iterable).last();
862865
}
863866

864867
return Iterators.getLast(iterable.iterator());
@@ -889,6 +892,8 @@ public Iterator<T> iterator() {
889892
return defaultValue;
890893
} else if (iterable instanceof List) {
891894
return getLastInNonemptyList((List<? extends T>) iterable);
895+
} else if (iterable instanceof SortedSet) {
896+
return ((SortedSet<? extends T>) iterable).last();
892897
}
893898
}
894899

guava-tests/test/com/google/common/collect/IterablesTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ public void testGetLast_withDefault_not_empty_list() {
889889
public void testGetLast_emptySortedSet() {
890890
SortedSet<String> sortedSet = ImmutableSortedSet.of();
891891
assertThrows(NoSuchElementException.class, () -> Iterables.getLast(sortedSet));
892+
assertEquals("c", Iterables.getLast(sortedSet, "c"));
892893
}
893894

894895
public void testGetLast_iterable() {

guava/src/com/google/common/collect/Iterables.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Queue;
3737
import java.util.RandomAccess;
3838
import java.util.Set;
39+
import java.util.SortedSet;
3940
import java.util.Spliterator;
4041
import java.util.function.Consumer;
4142
import java.util.stream.Stream;
@@ -849,6 +850,8 @@ public Spliterator<T> spliterator() {
849850
throw new NoSuchElementException();
850851
}
851852
return getLastInNonemptyList(list);
853+
} else if (iterable instanceof SortedSet) {
854+
return ((SortedSet<T>) iterable).last();
852855
}
853856

854857
return Iterators.getLast(iterable.iterator());
@@ -879,6 +882,8 @@ public Spliterator<T> spliterator() {
879882
return defaultValue;
880883
} else if (iterable instanceof List) {
881884
return getLastInNonemptyList((List<? extends T>) iterable);
885+
} else if (iterable instanceof SortedSet) {
886+
return ((SortedSet<? extends T>) iterable).last();
882887
}
883888
}
884889

0 commit comments

Comments
 (0)