Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Stream#toList() instead of collect() #311

Merged
merged 1 commit into from
Jan 8, 2024

Conversation

MariusVolkhart
Copy link
Contributor

toList was added in Java 16, and some streams can perform a more optimized operation compared to Collectors.toList().

toList was added in Java 16, and some streams can perform a more optimized operation compared to Collectors.toList().
Copy link
Contributor

@edward3h edward3h left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know that .toList() could be more efficient than Collectors.toList(), but I can believe it. Thanks!

@MariusVolkhart
Copy link
Contributor Author

Comment from Brian Goetz on some details. IIRC, Mark Stuart also had a presentation about it when talking about the Collections API, but I can't find it. :) https://stackoverflow.com/a/65981428/2502247

Copy link

codecov bot commented Jan 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (0c83398) 91.20% compared to head (2bee4d7) 91.14%.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #311      +/-   ##
============================================
- Coverage     91.20%   91.14%   -0.07%     
+ Complexity     1204     1203       -1     
============================================
  Files            76       76              
  Lines          3139     3139              
  Branches        489      489              
============================================
- Hits           2863     2861       -2     
- Misses          167      168       +1     
- Partials        109      110       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@casid
Copy link
Owner

casid commented Jan 8, 2024

I think the main difference is that collect(Collectors.toList()) currently returns a mutable ArrayList (but this could change anytime in future Java versions) and toList() returns an immutable list.

This change could potentially break user code for these public methods:

public List<String> generateAll()
public List<String> precompileAll()
public List<String> precompile(List<String> names)

If users attempt to modify the returned lists. Most regular users of the lib don't call these, so it shouldn't be a problem. We should still add a comment to the patch notes though.

@casid casid merged commit 08c5015 into casid:main Jan 8, 2024
8 checks passed
@MariusVolkhart MariusVolkhart deleted the mv/toList branch January 10, 2024 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants