|
List<String> result = data.stream().collect(Collectors.toList()); |
Change
List<String> result = data.stream().collect(Collectors.toList());
to
List<String> result = new ArrayList<>(data);
Collectors.toList() introduces extra Stream and Collector overhead, creating intermediate objects and lacking a guaranteed list type. In contrast, new ArrayList<>(collection) leverages bulk copy through toArray(), avoids unnecessary allocations, and deterministically produces an ArrayList. Therefore, when the source is already a Collection, replacing Collectors.toList() with new ArrayList<>(collection) yields more predictable behavior and better runtime performance.