Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/main/java/graphql/collect/ImmutableKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,27 @@ public static <K, V> ImmutableMap<K, V> mergeMaps(Map<K, V> m1, Map<K, V> m2) {
}

public static <T> ImmutableList<T> concatLists(List<T> l1, List<T> l2) {
return ImmutableList.<T>builder().addAll(l1).addAll(l2).build();
//noinspection UnstableApiUsage
return ImmutableList.<T>builderWithExpectedSize(l1.size() + l2.size()).addAll(l1).addAll(l2).build();
}

/**
* This is more efficient than `c.stream().map().collect()` because it does not create the intermediate objects needed
* for the flexible style. Benchmarking has shown this to outperform `stream()`.
*
* @param iterable the iterable to map
* @param collection the collection to map
* @param mapper the mapper function
* @param <T> for two
* @param <R> for result
*
* @return a map immutable list of results
*/
public static <T, R> ImmutableList<R> map(Iterable<? extends T> iterable, Function<? super T, ? extends R> mapper) {
assertNotNull(iterable);
public static <T, R> ImmutableList<R> map(Collection<? extends T> collection, Function<? super T, ? extends R> mapper) {
assertNotNull(collection);
assertNotNull(mapper);
@SuppressWarnings("RedundantTypeArguments")
ImmutableList.Builder<R> builder = ImmutableList.<R>builder();
for (T t : iterable) {
@SuppressWarnings({"RedundantTypeArguments", "UnstableApiUsage"})
ImmutableList.Builder<R> builder = ImmutableList.<R>builderWithExpectedSize(collection.size());
for (T t : collection) {
R r = mapper.apply(t);
builder.add(r);
}
Expand All @@ -101,6 +102,7 @@ public static <T> ImmutableList<T> addToList(Collection<? extends T> existing, T
assertNotNull(existing);
assertNotNull(newValue);
int expectedSize = existing.size() + 1 + extraValues.length;
@SuppressWarnings("UnstableApiUsage")
ImmutableList.Builder<T> newList = ImmutableList.builderWithExpectedSize(expectedSize);
newList.addAll(existing);
newList.add(newValue);
Expand All @@ -124,6 +126,7 @@ public static <T> ImmutableSet<T> addToSet(Collection<? extends T> existing, T n
assertNotNull(existing);
assertNotNull(newValue);
int expectedSize = existing.size() + 1 + extraValues.length;
@SuppressWarnings("UnstableApiUsage")
ImmutableSet.Builder<T> newSet = ImmutableSet.builderWithExpectedSize(expectedSize);
newSet.addAll(existing);
newSet.add(newValue);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/benchmark/ListBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static List<String> buildStartingList() {
return list;
}

Function<String, String> mapper = s -> new StringBuilder(s).reverse().toString();
private final Function<String, String> mapper = s -> new StringBuilder(s).reverse().toString();

@Benchmark
public void benchmarkListStream(Blackhole blackhole) {
Expand Down