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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:33:33+00:00 2026-06-04T00:33:33+00:00

Often I produce a collection on demand to save on instance data size. The

  • 0

Often I produce a collection on demand to save on instance data size. The consumer iterates through the collection probably only once, before its garbage collected. The consumer doesn’t care about the order of the collection doesn’t need to sort on it certainly doesn’t need to mutate it, or any of its elements. What’s the most efficient type safe collection in Scala? – an Array?

Later edit: It occurs to me that there are probably quite a few situations when I could use Sets. Is it good to use Sets when possible or just to use them when set functionality is really needed?

  • 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-04T00:33:35+00:00Added an answer on June 4, 2026 at 12:33 am

    Yes, of all the collection data structures, arrays come with the least amount of overhead when you know their size in advance.

    If you don’t know the size ahead of time, I would still pick an ArrayBuffer*. The algorithm used to expand the underlying array when it runs out of space is as efficient as it gets.

    Do not* use a (linked) List or a Stream because these classes involve one heap allocation per element. Modern JVM garbage collectors are good, but they don’t work for free.

    *: But see @user unknown‘s comment on the question for links to some micro-benchmarks. The current ArrayBuffer implementation might be suboptimal.

    Also have a look at .view. Often you don’t need to actually store intermediate results. Instead you can use .map, .filter and others to build up a “description” of a collection. The operations (map, filter, etc.) will only be performed when you iterate over the collection, often in O(1) space. The downside is, that these views will be re-computed every time they’re queried. (though this might still be more efficient with simple filters and huge underlying collections)

    Also, be extra careful with views on mutable data structures. Views don’t capture the state of the underlying data structure. When it changes, so does the view. Views on immutable data structures, however, behave very nicely. Finally, views obviously contain a reference to the underlying data structure, meaning it won’t be garbage collected while your program holds onto the view.

    (Updated)
    Vectors seem to strike a good balance between storage efficiency versus flexibility, especially for large sequences.

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

Sidebar

Related Questions

Every once in a while, downloading (especially large) files through ftp will produce errors.
Often, I would like to build up complex regexps from simpler ones. The only
I often write queries wherein I pivot data and end up with NULL values
I often find linq being problematic when working with custom collection object. They are
I often use Sweave to produce LaTeX documents where certain chunks are produced dynamically
Quite often Ruby installed with RVM (the 'most popular tool to install Ruby') produces
Often when I'm working on a project with others, the amount of library paths
Often when browsing in Firefox, I'll right click on a link, and open it
Often, when searching for answers, I have found that certain websites will allow you
Often, when restarting Django runserver, if I use the same port number, I get

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.