1

I am attempting to initialize a hashmap that takes in a String as its first parameter, and a set of Strings for the second parameter. Below is my attempt at creating this hashmap, but I am getting an unresolved reference to mutableSetOf are there any suggestions?

var map: HashMap<String, mutableSetOf<String>> = HashMap<String,mutableSetOf<String>>()

1 Answers1

2

mutableSetOf is a factory method that constructs a new mutable set. But you try to use it in the type signature of your map variable.

What you actually want is probably just

var map: HashMap<String, MutableSet<String>> = hashMapOf()

See https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-set-of.html

Lovis
  • 9,513
  • 5
  • 31
  • 47
  • 1
    Also, it's worth checking whether you specifically need a `HashMap`, or whether any `MutableMap` will do.  If the latter, then far better to use that in the variable's type — [program to the interface rather than to the implementation](/questions/2697783).  You can then initialise it with `mutableMapOf()`, which would benefit from any improvements the standard library adds. – gidds Jun 16 '21 at 20:20