conjure-java
conjure-java copied to clipboard
Accept Stream<T> in collection-like builder fields
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();
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();
@carterkozak Does Collections2.transform yield the same presizing benefits?
It does