Skip to content

Commit 5cac2e3

Browse files
authored
ImmutableKit.map with pre-size (#2841)
1 parent a006bd3 commit 5cac2e3

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/main/java/graphql/collect/ImmutableKit.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,27 @@ public static <K, V> ImmutableMap<K, V> mergeMaps(Map<K, V> m1, Map<K, V> m2) {
6161
}
6262

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

6768
/**
6869
* This is more efficient than `c.stream().map().collect()` because it does not create the intermediate objects needed
6970
* for the flexible style. Benchmarking has shown this to outperform `stream()`.
7071
*
71-
* @param iterable the iterable to map
72+
* @param collection the collection to map
7273
* @param mapper the mapper function
7374
* @param <T> for two
7475
* @param <R> for result
7576
*
7677
* @return a map immutable list of results
7778
*/
78-
public static <T, R> ImmutableList<R> map(Iterable<? extends T> iterable, Function<? super T, ? extends R> mapper) {
79-
assertNotNull(iterable);
79+
public static <T, R> ImmutableList<R> map(Collection<? extends T> collection, Function<? super T, ? extends R> mapper) {
80+
assertNotNull(collection);
8081
assertNotNull(mapper);
81-
@SuppressWarnings("RedundantTypeArguments")
82-
ImmutableList.Builder<R> builder = ImmutableList.<R>builder();
83-
for (T t : iterable) {
82+
@SuppressWarnings({"RedundantTypeArguments", "UnstableApiUsage"})
83+
ImmutableList.Builder<R> builder = ImmutableList.<R>builderWithExpectedSize(collection.size());
84+
for (T t : collection) {
8485
R r = mapper.apply(t);
8586
builder.add(r);
8687
}
@@ -101,6 +102,7 @@ public static <T> ImmutableList<T> addToList(Collection<? extends T> existing, T
101102
assertNotNull(existing);
102103
assertNotNull(newValue);
103104
int expectedSize = existing.size() + 1 + extraValues.length;
105+
@SuppressWarnings("UnstableApiUsage")
104106
ImmutableList.Builder<T> newList = ImmutableList.builderWithExpectedSize(expectedSize);
105107
newList.addAll(existing);
106108
newList.add(newValue);
@@ -124,6 +126,7 @@ public static <T> ImmutableSet<T> addToSet(Collection<? extends T> existing, T n
124126
assertNotNull(existing);
125127
assertNotNull(newValue);
126128
int expectedSize = existing.size() + 1 + extraValues.length;
129+
@SuppressWarnings("UnstableApiUsage")
127130
ImmutableSet.Builder<T> newSet = ImmutableSet.builderWithExpectedSize(expectedSize);
128131
newSet.addAll(existing);
129132
newSet.add(newValue);

src/test/java/benchmark/ListBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private static List<String> buildStartingList() {
3333
return list;
3434
}
3535

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

3838
@Benchmark
3939
public void benchmarkListStream(Blackhole blackhole) {

0 commit comments

Comments
 (0)