How does Scala's groupBy identity work?

后端 未结 6 1042
天涯浪人
天涯浪人 2021-02-01 05:56

I was browsing around and found a question about grouping a String by it\'s characters, such as this:

The input:

\"aaabbbccccdd\"
         


        
6条回答
  •  后悔当初
    2021-02-01 06:28

    Take a look at

    str.groupBy(identity)
    

    which returns

    scala.collection.immutable.Map[Char,String] = Map(b -> bbb, d -> dd, a -> aaa, c -> cccc)
    

    so the key by which the elements are grouped by is the character.

提交回复
热议问题