RFR 8233187: Return Empty List if 0 Copies Requested in Collections nCopies

Martin Buchholz martinrb at google.com
Thu Nov 14 15:34:05 UTC 2019


Hi Kiran, pleased to meet you

Code like this should be written so that the common case has only one
boolean test.

On Thu, Nov 14, 2019 at 1:50 AM Kiran Ravikumar <
kiran.sidhartha.ravikumar at oracle.com> wrote:

> Hi Guys,
>
>
> Please review the following optimization to nCopies method to return
> empty list and to avoid instantiating an empty CopiesList object.
>
>
> JBS link : https://bugs.openjdk.java.net/browse/JDK-8233187
>
>
> Patch:
>
> diff -r f279d8a9bb78 src/java.base/share/classes/java/util/Collections.java
> --- a/src/java.base/share/classes/java/util/Collections.java Wed Nov 13
> 11:27:50 2019 +0000
> +++ b/src/java.base/share/classes/java/util/Collections.java Thu Nov 14
> 09:37:56 2019 +0000
> @@ -5105,6 +5105,8 @@
>       public static <T> List<T> nCopies(int n, T o) {
>           if (n < 0)
>               throw new IllegalArgumentException("List length = " + n);
> +        if (n == 0)
> +            return Collections.emptyList();
>           return new CopiesList<>(n, o);
>       }
>
>
> Thanks,
>
> Kiran
>
>


More information about the core-libs-dev mailing list