Convert List of tuple to map (and deal with duplicate key ?)

前端 未结 8 2156
[愿得一人]
[愿得一人] 2020-12-12 14:32

I was thinking about a nice way to convert a List of tuple with duplicate key [(\"a\",\"b\"),(\"c\",\"d\"),(\"a\",\"f\")] into map (\"a\" -> [\"b\", \"

8条回答
  •  心在旅途
    2020-12-12 15:29

    For Googlers that don't expect duplicates or are fine with the default duplicate handling policy:

    List("a" -> 1, "b" -> 2).toMap
    // Result: Map(a -> 1, c -> 2)
    

    As of 2.12, the default policy reads:

    Duplicate keys will be overwritten by later keys: if this is an unordered collection, which key is in the resulting map is undefined.

提交回复
热议问题