RFR: 8286366: (cs) Charset.put can use putIfAbsent instead of containsKey+put

Alan Bateman alanb at openjdk.java.net
Sun May 15 13:52:47 UTC 2022


On Sun, 15 May 2022 10:23:11 GMT, Andrey Turbanov <aturbanov at openjdk.org> wrote:

>> src/java.base/share/classes/java/nio/charset/Charset.java line 569:
>> 
>>> 567:         while (i.hasNext()) {
>>> 568:             Charset cs = i.next();
>>> 569:             m.putIfAbsent(cs.name(), cs);
>> 
>> Yes, that would be a nicer. I've created JDK-8286366 for this. It could be changed to use for-each while you are there.
>
>> It could be changed to use for-each while you are there.
> 
> Not sure I get the idea.
> Do you mean something like this:
> 
>         i.forEachRemaining(new Consumer<Charset>() {
>             @Override
>             public void accept(Charset cs) {
>                 m.putIfAbsent(cs.name(), cs);
>             }
>         });

Sorry for the confusion, I thought I was looking at an Iterable at the time.

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

PR: https://git.openjdk.java.net/jdk/pull/8480


More information about the nio-dev mailing list