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

Add MoreCollectors.to*WithExpectedSize helpers #173

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

schlosna
Copy link
Contributor

WIP Draft for comments

Some questions came up about providing Stream collectors that avoid some of the growth & reallocation overhead of JDK standard collectors such as Collectors.toList() and where libraries are targeting JDK 11 so cannot use Stream::toList() added in JDK 16.

This typically only matters in more allocation sensitive code in which case use of Guava
Collections2.transform, Lists.transform, Maps.transformValues may be better solutions to avoid the intermediate collection copy; however, there are some cases where one must make a copy (e.g. if the elements being transformed are only valid inside a transaction), so providing these mechanisms may be beneficial.

Questions:

  • Do we want to encourage use of these?
    • If used with filter, these likely over-allocate in what one might assume are performance/allocation sensitive paths.
    • For the places these matter, should we be encouraging other non-Stream implementation?
  • Are these worth the API burden & support?
  • Is this the place we'd want to land these?

@changelog-app
Copy link

changelog-app bot commented Apr 20, 2023

Generate changelog in changelog/@unreleased

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Add MoreCollectors.to*WithExpectedSize helpers

Check the box to generate changelog(s)

  • Generate changelog entry

@schlosna
Copy link
Contributor Author

schlosna commented Jun 3, 2023

Curious for thoughts on these additions given some recent places these could be used

* This collector has similar semantics to {@link ImmutableList#toImmutableList()}; however,
* the builder will be presized with the expected size to avoid resizing while collecting.
*/
public static <T> Collector<T, ?, ImmutableList<T>> toImmutableListWithExpectedSize(int expectedSize) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably want to steer folks toward Stream#toList() in JDK 17+

@schlosna schlosna marked this pull request as ready for review October 2, 2024 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants