scalacollectionsscala-collectionsscalastyle

Reassign ArrayList in a better Way


The below code is working, but its looking odd to me, is there any better way to this.

var res:scala.collection.mutable.LinkedHashMap[String,scala.collection.immutable.Map[String,String]]=??
var arList = new ArrayList[String]()
res.keySet.map(arList.add(_))
//here res key set changed so i want to reassign the list by new keySet
res=??                                                        //updated
arList.clear
res.keySet.map(arList.add(_))

its looking very odd that to call the .clear on arList


Solution

  • You can use default JavaConverters.

      import scala.collection.JavaConverters._
      val list = new java.util.ArrayList(res.keySet.asJavaCollection)
    

    I didn't get why do you need to do clear, is it some requirement that you pass ArrayList once and update it's content later?