There are two parts to this problem.
Retrieving data from a database and a list of strings.
I would use Spring SimpleJdbcOperations to access the database, so that things at least seem functional, although the ResultSet is changing behind the scenes.
Firstly, some simple conversion that allows us to use closure to display each row:
implicit def rowMapper[T<:AnyRef](func: (ResultSet)=>T) = new ParameterizedRowMapper[T]{ override def mapRow(rs:ResultSet, row:Int):T = func(rs) }
Then we define the data structure for storing the results. (You can use a tuple, but defining my own case class has the advantage of being a little more understandable with respect to the names of things.)
case class CategoryValue(category:String, property:String)
Now select from the database
val db:SimpleJdbcOperations = //get this somehow val resultList:java.util.List[CategoryValue] = db.query("select category, property from category_value", { rs:ResultSet => CategoryValue(rs.getString(1),rs.getString(2)) } )
Convert data from a list of rows to the desired format
import scala.collection.JavaConversions._ val result:Map[String,Set[String]] = resultList.groupBy(_.category).mapValues(_.map(_.property).toSet)
(You can omit type annotations. I have included them to make it clear what is happening.)
source share