[PATCH] Implement a noop clear() for Collections#EMPTY_LIST

Mohamed Naufal naufal11 at gmail.com
Sun May 22 10:10:41 UTC 2016


Hi,

A call to clear() on Collections#EMPTY_LIST is currently redirected to
AbstractList#clear(), which performs a bunch of checks and creates a
ListItr object, all of which is unnecessary for an EmptyList.

PFA a patch that implements a noop clear() for EmptyList.

Thanks,
Naufal
-------------- next part --------------
# HG changeset patch
# User Mohamed Naufal <naufal11 at gmail.com>
# Date 1463909563 -19800
#      Sun May 22 15:02:43 2016 +0530
# Node ID df323f5c5f30ae3443063d5a2c40ee780cacce78
# Parent  f8a3c1510f9525398d41bce83d79265762288e18
Collections: Implement a noop clear() for EmptyList

diff -r f8a3c1510f95 -r df323f5c5f30 src/java.base/share/classes/java/util/Collections.java
--- a/src/java.base/share/classes/java/util/Collections.java	Thu May 19 17:48:02 2016 +0000
+++ b/src/java.base/share/classes/java/util/Collections.java	Sun May 22 15:02:43 2016 +0530
@@ -4447,6 +4447,7 @@
 
         public int size() {return 0;}
         public boolean isEmpty() {return true;}
+        public void clear() {}
 
         public boolean contains(Object obj) {return false;}
         public boolean containsAll(Collection<?> c) { return c.isEmpty(); }


More information about the core-libs-dev mailing list