Reputation: 669
I have a typesafe config with a list of lists. Basically what I'm trying to do is to extract a multi-map.
myconfig {
values = [
[ 'key1', 'value1'],
[ 'key1', 'value2'],
[ 'key2', 'value2'],
[ 'key2', 'value3'],
]
Workaround I has to use:
myconfig {
values = {
key1 : [ 'value1' , 'value2' ]
key2 : [ 'value2' , 'value3' ]
}
}
Is there a nice way of extracting the original list of lists? All functions expect a path, so once I extract 'values' there seem to be no direct way of accessing an inner list.
Upvotes: 2
Views: 9723
Reputation: 1373
You can use the getList(String path)
on the Config
object to get back a ConfigList
object.
Java:
config.getList("path.to.keys").stream()
.map(configValue -> (ArrayList) configValue.unwrapped())
.collect(Collectors.toList())
A ConfigList
contains ConfigValue
instances. In this case, the typesafe people represent the list internally as an ArrayList so you have to make the cast when you unwrap the ConfigValue
object.
The snippet above returns a List of List. If you want to flatten the list, use flatMap instead of Map and return a stream in the lambda.
Edit
Here's the equivalent Scala:
import scala.collection.JavaConversions._
val keys: Map[String, String] = config.getList("path.to.keys")
.map(configValue =>
configValue.unwrapped().asInstanceOf[ArrayList[String]]
)
.foldLeft(Map[String, String]())( (map, list) =>
map + (list(0) -> list(1))
)
Upvotes: 5