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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:02:35+00:00 2026-05-31T00:02:35+00:00

The groupBy method in Lists, Maps, etc., generate a Map after the function. Is

  • 0

The groupBy method in Lists, Maps, etc., generate a Map after the function.

Is there a way to use the groupBy to generate a Map that preserves insertion order (LinkedHashMap, for instance)?

I’m using for loops to insert manually, but I wanted to know if one of the useful already-defined functions could help me.

Thanks in advance.

  • 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-31T00:02:36+00:00Added an answer on May 31, 2026 at 12:02 am

    groupBy as defined on TraversableLike produces an immutable.Map, so you can’t make this method produce something else.

    The order of the elements in each entry is already preserved, but not the order of the keys. The keys are the result of the function supplied, so they don’t really have an order.

    If you wanted to make an order based on the first occurrence of a particular key, here’s a sketch of how you might do it. Say we want to group integers by their value / 2:

    val m = List(4, 0, 5, 1, 2, 6, 3).zipWithIndex groupBy (_._1 / 2)
    val lhm = LinkedHashMap(m.toSeq sortBy (_._2.head._2): _*)
    lhm mapValues (_ map (_._1))
    // Map(2 -> List(4, 5), 0 -> List(0, 1), 1 -> List(2, 3), 3 -> List(6))
    // Note order of keys is same as first occurrence in original list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed that LINQ to Objects has a GroupBy method. In light of this,
Is there a way to create LINQ query that will have different grouping fields.
I have the following query method that has slow performance: @Override public Map<String, Long>
I would like to use the group_by method, but instead of using a column
Trying to work with groupby so that I can group together files that were
What would be the correct way to add DISTINCT and/or GROUPBY to ContentResolver -based
I have a method that currently takes a Func<Product, string> as a parameter, but
I'm refactoring some code and I have written a method that modifies a Dictionary
I'm trying to write a method that generates multiple invoices. This is for a
I'm trying to write a method that generates multiple invoices. This is for a

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.