Skip to content

Instantly share code, notes, and snippets.

@JakeWharton
Created March 24, 2014 04:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakeWharton/136a9b3cd09d21938d3e to your computer and use it in GitHub Desktop.
Save JakeWharton/136a9b3cd09d21938d3e to your computer and use it in GitHub Desktop.
Example Java 8 collectors for Guava types without method references or lambda expressions. https://gist.github.com/JakeWharton/9734167
/*
* Copyright (C) 2014 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import static java.util.stream.Collector.Characteristics.UNORDERED;
/** Stream {@link Collector collectors} for Guava types. */
public final class GuavaCollectors {
/** Collect a stream of elements into an {@link ImmutableList}. */
public static <T> Collector<T, ImmutableList.Builder<T>, ImmutableList<T>> immutableList() {
return Collector.of(new Supplier<ImmutableList.Builder<T>>() {
@Override public ImmutableList.Builder<T> get() {
return new ImmutableList.Builder<>();
}
}, new BiConsumer<ImmutableList.Builder<T>, T>() {
@Override public void accept(ImmutableList.Builder<T> builder, T item) {
builder.add(item);
}
}, new BinaryOperator<ImmutableList.Builder<T>>() {
@Override public ImmutableList.Builder<T> apply(ImmutableList.Builder<T> builder1,
ImmutableList.Builder<T> builder2) {
return builder1.addAll(builder2.build());
}
}, new Function<ImmutableList.Builder<T>, ImmutableList<T>>() {
@Override public ImmutableList<T> apply(ImmutableList.Builder<T> builder) {
return builder.build();
}
});
}
/** Collect a stream of elements into an {@link ImmutableSet}. */
public static <T> Collector<T, ImmutableSet.Builder<T>, ImmutableSet<T>> immutableSet() {
return Collector.of(new Supplier<ImmutableSet.Builder<T>>() {
@Override public ImmutableSet.Builder<T> get() {
return new ImmutableSet.Builder<>();
}
}, new BiConsumer<ImmutableSet.Builder<T>, T>() {
@Override public void accept(ImmutableSet.Builder<T> builder, T item) {
builder.add(item);
}
}, new BinaryOperator<ImmutableSet.Builder<T>>() {
@Override public ImmutableSet.Builder<T> apply(ImmutableSet.Builder<T> builder1,
ImmutableSet.Builder<T> builder2) {
return builder1.addAll(builder2.build());
}
}, new Function<ImmutableSet.Builder<T>, ImmutableSet<T>>() {
@Override public ImmutableSet<T> apply(ImmutableSet.Builder<T> builder) {
return builder.build();
}
}, UNORDERED);
}
private GuavaCollectors() {
throw new AssertionError("No instances.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment