3

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);
}
3
  • what is the Object in this case? Commented Dec 20, 2016 at 20:09
  • I guess in would be considered Any in scala. It is "anything" untyped for now. Commented Dec 20, 2016 at 20:10
  • An Enumeration is a sequence, how do you want a Map from that? Commented Dec 20, 2016 at 21:14

1 Answer 1

4

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  
Sign up to request clarification or add additional context in comments.

1 Comment

@coolbreeze has this answered your question? Consider accepting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.