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 8066895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:07:52+00:00 2026-06-05T12:07:52+00:00

I would like to take a list of RawDoc which each have a title

  • 0

I would like to take a list of RawDoc which each have a title and a single version and transform it into a list of Doc which each have a title and all its versions collected together into a list:

case class RawDoc(title:String, version:String)
case class Doc(title:String, versions:List[String])

val rawDocs:List[RawDoc] = List(
  RawDoc("Green Book", "1.1"),
  RawDoc("Blue Book",  "1.0"),
  RawDoc("Green Book", "1"),
  RawDoc("Blue Book",  "2")
)

I would like to start with the above rawDocs and create docs like this:

val docs:List[Doc] = List(
  Doc("Green Book", List("1.1", "1")),
  Doc("Blue Book",  List("1.0", "2"))
)

Without using for loops how could this be done in Scala?

  • 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-05T12:07:53+00:00Added an answer on June 5, 2026 at 12:07 pm

    This should work:

    val docs = rawDocs.
      groupBy(_.title).map{
        case(title, docsWithSameTitle) => 
          Doc(title, docsWithSameTitle.map(_.version))
      }
    

    And if the difference between "Blue Book" and "Blue BooK" is not incidental typo and they should be treated as equal:

    val docs = rawDocs.
      groupBy(_.title.toUpperCase).map{
        case(_, docsWithSameTitle) => 
          Doc(docsWithSameTitle.head.title, docsWithSameTitle.map(_.version))
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two ICollection s of which I would like to take the union.
I have a matrix in R that I would like to take a single
Hi I would like to take a list collection and generate a single csv
I would like to take a list that I have in a text file,
I have a List of strings (GUIDS) and I would like to take one
I have a long list of elements which I would like to scroll while
I have a list of name/value pairs that I would like to map into
I would like to take my list of tuples, where each tuple consists of
I would like to take a field and replace all characters that are not
I would like to take a passed List that I know is homogeneous and

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.