简体   繁体   English

groupingBy to Collection Java8

[英]groupingBy to Collection Java8

My method looks like this: 我的方法看起来像这样:

 public static <T, E>  Map<E, List<T>> groupBy(Collection<T> collection, Function<T, E> function){
    return collection.stream().collect(Collectors.groupingBy(function::apply));
}

And it works, but i want my method to look like this: 它的工作原理,但我希望我的方法看起来像这样:

public static <T, E>  Map<E, Collection<T>> groupBy(Collection<T> collection, Function<T, E> function){
    return collection.stream().collect(Collectors.groupingBy(function::apply));
}

And then it doesnt work, because groupingBy returns Map < E, List< T>>, not Map < E, Collection< T>>. 然后它不起作用,因为groupingBy返回Map <E,List <T >>,而不是Map <E,Collection <T >>。

Of course i can do it like this: 我当然可以这样做:

   public static <T, E>  Map<E, Collection<T>> groupBy(Collection<T> collection, Function<T, E> function){
    return (Map<E, Collection<T>>) (Map) collection.stream().collect(Collectors.groupingBy(function::apply));
}

but I don't like this solution. 但我不喜欢这个解决方案。 Also method declaration: 方法声明:

    public static <T, E>  Map<E, Collection<T>> groupBy(Collection<T> collection, Function<T, E> function)

HAS TO stay that way (that was part of the task). 必须保持这种方式(这是任务的一部分)。 So is there any better solutions than casting? 那么有什么比铸造更好的解决方案? I know there is second version of groupingBy which as a second paramether it takes Collector which can change type of the map which groupingBy returns. 我知道有第二个版本的groupingBy作为第二个参数,它需要收集器,它可以改变groupingBy返回的地图类型。 However I don't know how to use it. 但是我不知道如何使用它。 There is Collectors.toCollection() , but I think it will still be returning some implementation of Collection, not Collection itself. Collectors.toCollection() ,但我认为它仍将返回Collection的一些实现,而不是Collection本身。

There is also possibility of writing my custom Collector, following this link: http://www.nurkiewicz.com/2014/07/introduction-to-writing-custom.html 也可以按照以下链接编写我的自定义收集器: http//www.nurkiewicz.com/2014/07/introduction-to-writing-custom.html

but i don't know how to use it with Collection. 但我不知道如何将它与Collection一起使用。

I would really apprieciate your help, thanks :) 我真的很感谢你的帮助,谢谢:)

Something like this should work: 这样的事情应该有效:

public static <T, E>  Map<E, Collection<T>> groupBy(Collection<T> collection, Function<T, E> function){
    return collection.stream()
                     .collect(Collectors.groupingBy(function,
                                                    Collectors.toCollection(ArrayList::new)));
}

This would produce an ArrayList instance for each value in the Map . 这将为Map每个值生成一个ArrayList实例。 You can choose other collections if you wish (just change ArrayList::new ). 如果您愿意,可以选择其他集合(只需更改ArrayList::new )。

You can provide the type of collector and also the type of Map: 您可以提供收集器的类型以及Map的类型:

public static <T, E> Map<E, Collection<T>> groupBy(Collection<T> collection, Function<T, E> function){
    return collection.stream().collect(Collectors.groupingBy(function::apply, HashMap::new,Collectors.toCollection(ArrayList::new)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM