RFR: 8368178: Add specialization of SequencedCollection methods to emptyList, singletonList and nCopies

Pavel Rappo prappo at openjdk.org
Sat Sep 20 20:39:12 UTC 2025


On Sat, 20 Sep 2025 19:29:11 GMT, Tagir F. Valeev <tvaleev at openjdk.org> wrote:

> If you have more ideas which classes may miss specializations of SequencedCollection methods, I can add them to this PR as well.

Have you considered something like this?

diff --git a/src/java.base/share/classes/java/util/ImmutableCollections.java b/src/java.base/share/classes/java/util/ImmutableCollections.java
index 38cc45122a2..d8c3499a958 100644
--- a/src/java.base/share/classes/java/util/ImmutableCollections.java
+++ b/src/java.base/share/classes/java/util/ImmutableCollections.java
@@ -352,7 +352,7 @@ public boolean contains(Object o) {
 
         @Override
         public List<E> reversed() {
-            return ReverseOrderListView.of(this, false);
+            return size() < 2 ? this : ReverseOrderListView.of(this, false);
         }
 
         IndexOutOfBoundsException outOfBounds(int index) {
@@ -636,6 +636,11 @@ public int lastIndexOf(Object o) {
             }
         }
 
+        @Override
+        public List<E> reversed() {
+            return size() == 1 ? this : super.reversed();
+        }
+
         @java.io.Serial
         private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
             throw new InvalidObjectException("not serial proxy");

-------------

PR Comment: https://git.openjdk.org/jdk/pull/27406#issuecomment-3315244733


More information about the core-libs-dev mailing list