Stream流根据某一属性分组

用法

  list.stream().collect(Collectors.groupingBy(entity -> entity.getParam()))

eg :

  {type = "类型一",name = 1}

  {type = "类型一",name = 2}

  {type = "类型二",name = 3}

  {type = "类型二",name = 4}

  {type = "类型三",name = 5}

  {type = "类型三",name = 6}

 

得到的结果 :Map    {

            "类型一":

                [{type = "类型一",name = 1},{type = "类型一",name = 2}] ,

            "类型二":

                [{type = "类型二",name = 3},{type = "类型二",name = 4}] ,

            "类型三":

                [{type = "类型三",name = 5},{type = "类型三",name = 6}] 

          }

posted @ 2021-12-09 10:55  DarkerbeS  阅读(1264)  评论(0编辑  收藏  举报