Collectors.groupingBy doesn't accept null keys

前端 未结 6 2040
囚心锁ツ
囚心锁ツ 2020-12-15 02:49

In Java 8, this works:

Stream stream = Stream.of(ArrayList.class);
HashMap> map = (HashMap)stream.collect(Collect         


        
6条回答
  •  囚心锁ツ
    2020-12-15 03:23

    To your 1st question, from the docs:

    There are no guarantees on the type, mutability, serializability, or thread-safety of the Map or List objects returned.

    Because not all Map implementations allow null keys they probably added this to reduce to the most common allowable definition of a map to get maximum flexibility when choosing a type.

    To your 2nd question, you just need a supplier, wouldn't a lambda work? I'm still getting acquainted with Java 8, maybe a smarter person can add a better answer.

提交回复
热议问题