Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a Java enumeration to a Scala Map

Tags:

scala

Is there a way to take a java based API that loops over property names and someone add the key/value pairs to a Scala Map[String, Object] ?

Enumeration properties = something.getPropertyNames();
while (properties.hasMoreElements())
{
    String propName = (String) srcProperties.nextElement();
    Object v = something.getValue(propName);
}
like image 255
cool breeze Avatar asked Dec 20 '25 15:12

cool breeze


1 Answers

I think you are confusing a Java Enumeration with the Properties object it was obtained from. Like in Enumeration propertyNames = props.getPropertyNames()

We cannot create a map from the resulting Enumeration, because it's only a sequence of Strings, but we can obtain a Map[String,String] from the original Properties object.

 import scala.collection.JavaConverters._
 val propertyMap = props.asScala  // mutable map
 // or
 val propertyMap = props.asScala.toMap  // immutable map  
like image 178
maasg Avatar answered Dec 23 '25 12:12

maasg