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

Accept Stream<T> in collection-like builder fields #519

Open
henryptung opened this issue Aug 23, 2019 · 3 comments
Open

Accept Stream<T> in collection-like builder fields #519

henryptung opened this issue Aug 23, 2019 · 3 comments

Comments

@henryptung
Copy link

henryptung commented Aug 23, 2019

Since Conjure always makes a defensive copy and the element type may need conversion, accepting only Iterable<T> encourages creation of a "waste" copy:

Bean bean = Bean.builder()
        .values(myValues.stream().map(Converter::convert).collect(toList()))
        .build();

Instead, this could accept the stream directly, use .forEachOrdered and avoid the extra copy:

Bean bean = Bean.builder()
        .values(myValues.stream().map(Converter::convert))
        .build();
@carterkozak
Copy link
Contributor

This can be done much more efficiently using Lists.transform which avoids stream overhead, and allows the conjure builder to presize the internal collection.

Bean bean = Bean.builder()
        .values(Lists.transform(myValues, Converter::convert))
        .build();

The following also works, but not in a fluent way:

Bean.Builder builder = Bean.builder();
myValues.stream().map(Converter::convert).forEach(builder::values));
Bean bean = builder.build();

@henryptung
Copy link
Author

@carterkozak Does Collections2.transform yield the same presizing benefits?

@carterkozak
Copy link
Contributor

It does

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

No branches or pull requests

2 participants