Last active
December 24, 2015 02:12
-
-
Save pinzolo/13ccbf48c1127377d6b2 to your computer and use it in GitHub Desktop.
重複要素を考慮した Collector クラス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pinzolo.util.collectors; | |
import java.util.Collections; | |
import java.util.EnumSet; | |
import java.util.HashSet; | |
import java.util.Set; | |
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; | |
public class DistinctCountingCollector<T> implements Collector<T, Set<T>, Long> { | |
@Override | |
public Supplier<Set<T>> supplier() { | |
return HashSet<T>::new; | |
} | |
@Override | |
public BiConsumer<Set<T>, T> accumulator() { | |
return (set, obj) -> set.add(obj); | |
} | |
@Override | |
public BinaryOperator<Set<T>> combiner() { | |
return (set1, set2) -> { | |
set1.addAll(set2); | |
return set1; | |
}; | |
} | |
@Override | |
public Function<Set<T>, Long> finisher() { | |
return set -> Long.valueOf(set.size()); | |
} | |
@Override | |
public Set<Collector.Characteristics> characteristics() { | |
return Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.UNORDERED)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment