code: public static <T, U> Collector<T, ?, Long> countDistinctBy(Function<? super T, ? extends U> function) {
          return Collector.of(
              () -> new HashSet<U>(),
              (s, v) -> s.add(function.apply(v)),
              (s1, s2) -> {
                  s1.addAll(s2);
                  return s1;
              },
              s -> (long) s.size()
          );
      }
docs: Get a {@link Collector} that calculates the
  <code>COUNT (DISTINCT expr)</code> function.