-1

I saw this Scala syntax and I'm don't understand what it means:

val a: HashMap[String, String] = HashMap(b map {s => (s(0), s(1))} : _*)

I understand that it takes a list of pairs and converts it to a hashmap keyed by the first element of each pair, and the value is the second element of the pair. However, I don't understand how this syntax implies that, and what the _* means.

user1742188
  • 4,563
  • 8
  • 35
  • 60

1 Answers1

2

Firstly, s => (s(0), s(1)) creates a tuple of type (String, String). Secondly, _ is a wild card for the aforementioned tuple's type (String, String). Then, with * you indicate that it is a vararg (repeated parameter).

So in other words, it means that you pass an Array of type _ with variable number of parameters (*) that was created from maping collection b into tuple (String, String) (represented as _).

sebszyller
  • 853
  • 4
  • 10