Skip to content

Replace Collectors.toList() with new ArrayList<>(Collection) in random name selection #3

Description

@SaFE-APIOpt

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions