Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3953014
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:55:25+00:00 2026-05-20T01:55:25+00:00

I have a SQL database table with the following structure: create table category_value (

  • 0

I have a SQL database table with the following structure:

create table category_value (
  category varchar(25),
  property varchar(25)
);

I want to read this into a Scala Map[String, Set[String]] where each entry in the map is a set of all of the property values that are in the same category.
I would like to do it in a “functional” style with no mutable data (other than the database result set).

Following on the Clojure loop construct, here is what I have come up with:

def fillMap(statement: java.sql.Statement): Map[String, Set[String]] = {
    val resultSet = statement.executeQuery("select category, property from category_value")

    @tailrec
    def loop(m: Map[String, Set[String]]): Map[String, Set[String]] = {
      if (resultSet.next) {
        val category = resultSet.getString("category")
        val property = resultSet.getString("property")
        loop(m + (category -> m.getOrElse(category, Set.empty)))
      } else m
    }

    loop(Map.empty)
}

Is there a better way to do this, without using mutable data structures?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:55:25+00:00Added an answer on May 20, 2026 at 1:55 am

    If you like, you could try something around

    def fillMap(statement: java.sql.Statement): Map[String, Set[String]] = {
      val resultSet = statement.executeQuery("select category, property from category_value")
      Iterator.continually((resultSet, resultSet.next)).takeWhile(_._2).map(_._1).map{ res =>
        val category = res.getString("category")
        val property = res.getString("property")
        (category, property)
      }.toIterable.groupBy(_._1).mapValues(_.map(_._2).toSet)
    }
    

    Untested, because I don’t have a proper sql.Statement. And the groupBy part might need some more love to look nice.

    Edit: Added the requested changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table structure in my database: create table Encargado( ID int
I have the following database structure: CREATE TABLE LookupTable ( PK UNIQUEIDENTIFIER PRIMARY KEY,
i have got a problem with my SQL Statement: Table structure: CREATE TABLE tags
Given the following simple table structure (SQL Server 2008), I want to be able
I have an SQL Server database table created by deploying the following description in
I have a SQL Mobile database with one table. It has several columns with
In a SQL server database, I have a table which contains a TEXT field
I have a field (say, foo) in a table in a SQL Server database
I have a table in a SQL Server 2005 database with a trigger that
Lets say I have a table in a sql server 2000 database called TransactionType:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.