How would you make a java.util.stream.Collector that collects to com.google.common.collect.ImmutableSet?
问题 That seems too tricky for me since ImmutableSet instances are only built with ImmutableSet.Builder instances, which don't implement Collection so you can't just use Collectors.toCollection(ImmutableSet::new) or Collectors.toCollection(ImmutableSet.Builder::new) . 回答1: This is built into guava now, ImmutableSet#toImmutableSet Use like, something.stream().collect(ImmutableSet.toImmutableSet()) 回答2: In fact, 3 months after :-), instead of defining a whole class for this, you can use Collector.of