code: public static <T, K, R> Map<R, List<T>> toMapGroupBy( Stream<T> stream, Function<T, R> groupingFunction ) {
          return stream.collect(Collectors.groupingBy(groupingFunction));
          // to get a set: Collectors.groupingBy(groupingFunction, Collectors.toSet())
      }
docs: Collect a map by grouping based on the object's field.
  
  @param stream the stream to collect.
  @param groupingFunction the function supplying the grouping field, ex. Object::getField()
  @return the map of lists.