RFR: JDK-8263411: Convert jshell tool to use Stream.toList()

Stuart Marks smarks at openjdk.java.net
Mon Mar 15 23:39:14 UTC 2021


On Fri, 12 Mar 2021 21:51:07 GMT, Ian Graves <igraves at openjdk.org> wrote:

> This converts jshell from using `Stream.collect(Collectiors.toList())` to `Stream.toList()` - an immutable list with better performance characteristics. Local inspection only turned up one place that was mutating a resulting list and that has been refactored. 
> 
> This work is a subtask to: https://bugs.openjdk.java.net/browse/JDK-8260559

src/jdk.jshell/share/classes/jdk/jshell/SnippetMaps.java line 177:

> 175:                                .map(isi -> isi.fullname.substring(0, isi.fullname.lastIndexOf(".")));
> 176:         pkgs = Stream.concat(Stream.of("java.lang"), pkgs);
> 177:         for (String ipkg : pkgs.toList()) {

The original code is strictly speaking in error, as it's modifying the list returned from `Collectors.toList()` which is not guaranteed to be mutable. (Obviously, this isn't enforced.) Instead of concatenating the element to the head of the stream and using `Stream.toList()`, though, it might be just as easy to convert the original code to use `collect(Collectors.toCollection(ArrayList::new))` and leave the insertion of "java.lang" at the front at before. Might make things clearer as to what's going on.

(A slight variation would be to static-import j.u.s.Collectors.toCollection.)

I'll defer to the jshell team on their preference here.

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

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


More information about the kulla-dev mailing list