guava/src/com/google/common/collect/ImmutableSetMultimap.java
public static <T, K, V>
      Collector<T, ?, ImmutableSetMultimap<K, V>> flatteningToImmutableSetMultimap(
          Function<? super T, ? extends K> keyFunction,
          Function<? super T, ? extends Stream<? extends V>> valuesFunction) {
    checkNotNull(keyFunction);
    checkNotNull(valuesFunction);
    return Collectors.collectingAndThen(
        Multimaps.flatteningToMultimap(
            input -> checkNotNull(keyFunction.apply(input)),
            input -> valuesFunction.apply(input).peek(Preconditions::checkNotNull),
            MultimapBuilder.linkedHashKeys().linkedHashSetValues()::<K, V>build),
        ImmutableSetMultimap::copyOf);
  }
public static <T, K, V>
      Collector<T, ?, ImmutableSetMultimap<K, V>> flatteningToImmutableSetMultimap(
          Function<? super T, ? extends K> keyFunction,
          Function<? super T, ? extends Stream<? extends V>> valuesFunction) {
    checkNotNull(keyFunction);
    checkNotNull(valuesFunction);
    return Collectors.collectingAndThen(
        Multimaps.flatteningToMultimap(
            input -> checkNotNull(keyFunction.apply(input)),
            input -> valuesFunction.apply(input).peek(Preconditions::checkNotNull),
            MultimapBuilder.linkedHashKeys().linkedHashSetValues()::<K, V>build),
        ImmutableSetMultimap::copyOf);
  }
Returns a {@code Collector} accumulating entries into an {@code ImmutableSetMultimap}. Each
input element is mapped to a key and a stream of values, each of which are put into the
resulting {@code Multimap}, in the encounter order of the stream and the encounter order of the
streams of values.

<p>Example:

<pre>{@code
static final ImmutableSetMultimap<Character, Character> FIRST_LETTER_MULTIMAP =
Stream.of("banana", "apple", "carrot", "asparagus", "cherry")
.collect(
flatteningToImmutableSetMultimap(
str -> str.charAt(0),
str -> str.substring(1).chars().mapToObj(c -> (char) c));

// is equivalent to

static final ImmutableSetMultimap<Character, Character> FIRST_LETTER_MULTIMAP =
ImmutableSetMultimap.<Character, Character>builder()
.putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a'))
.putAll('a', Arrays.asList('p', 'p', 'l', 'e'))
.putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't'))
.putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's'))
.putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y'))
.build();

// after deduplication, the resulting multimap is equivalent to

static final ImmutableSetMultimap<Character, Character> FIRST_LETTER_MULTIMAP =
ImmutableSetMultimap.<Character, Character>builder()
.putAll('b', Arrays.asList('a', 'n'))
.putAll('a', Arrays.asList('p', 'l', 'e', 's', 'a', 'r', 'g', 'u'))
.putAll('c', Arrays.asList('a', 'r', 'o', 't', 'h', 'e', 'y'))
.build();
}
}</pre>

@since 21.0