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

  • SEARCH
  • Home
  • 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 7929617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:08:27+00:00 2026-06-03T20:08:27+00:00

I know this is probably a very simple List operation in Scala, but I’m

  • 0

I know this is probably a very simple List operation in Scala, but I’m a newbie and can’t figure it out. I have a query that returns a result set with a series of values, grouped by a common id. For example:

Result Set:

[{ 1, "a", 30 },
{ 1, "b", 20 },
{ 1, "c", 22 },
{ 2, "a", 32 },
{ 2, "c", 10 }]

and what I’d like to do is put this into a map as such:

1 -> [{"a", 30}, {"b", 20}, {"c", 22}]
2 -> [{"a", 32}, {"c", 10}]

I think the collect method can be used for this but can’t figure it out.

  • 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-06-03T20:08:30+00:00Added an answer on June 3, 2026 at 8:08 pm

    I’m not sure what the types in your data structure are, but maybe you can adapt this. This assumes you have a collection of tuples:

    val items = 
      List((1, "a", 30),
           (1, "b", 20),
           (1, "c", 22),
           (2, "a", 32),
           (2, "c", 10))
    
    items
      .groupBy{ case (a,b,c) => a }
      .mapValues(_.map{ case (a,b,c) => (b,c) })
    
    // Map(1 -> List((a,30), (b,20), (c,22)), 2 -> List((a,32), (c,10)))
    

    Or, more succinctly:

    items.groupBy(_._1).mapValues(_.map(t => (t._2, t._3)))
    

    The collect method is something else entirely (basically, it’s map that drops non-matching values). The groupBy method is what you were really looking for.

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

Sidebar

Related Questions

I know this is probably really dumb, but I can't figure out why this
I know this is probably a very simple question, but I can't seem to
I know this is probably a very simple question but how would I do
I know this is probably really obvious, but I cannot figure out why I
I know this is probably a really amateur question, but I can't figure this
Okay, this is (probably) a very simple question, but I am afraid I know
Okay, this is probably very simple but, I have the below checks (not at
This is probably a very simple question but I have a confusion about the
I know this probably really simple but Im not sure what im doing wrong...
I know this is probably on the Internet somewhere but I can't find the

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.