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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:44:54+00:00 2026-05-26T07:44:54+00:00

Given a large collection (let’s call it ‘a’) of elements of type T (say,

  • 0

Given a large collection (let’s call it ‘a’) of elements of type T (say, a Vector or List) and an evaluation function ‘f’ (say, (T) => Double) I would like to derive from ‘a’ a result collection ‘b’ that contains the N elements of ‘a’ that result in the highest value under f. The collection ‘a’ may contain duplicates. It is not sorted.

Maybe leaving the question of parallelizability (map/reduce etc.) aside for a moment, what would be the appropriate Scala data structure for compiling the result collection ‘b’? Thanks for any pointers / ideas.

Notes:

(1) I guess my use case can be most concisely expressed as

val a = Vector( 9,2,6,1,7,5,2,6,9 ) // just an example
val f : (Int)=>Double = (n)=>n      // evaluation function
val b = a.sortBy( f ).take( N )     // sort, then clip

except that I do not want to sort the entire set.

(2) one option might be an iteration over ‘a’ that fills a TreeSet with ‘manual’ size bounding (reject anything worse than the worst item in the set, don’t let the set grow beyond N). However, I would like to retain duplicates present in the original set in the result set, and so this may not work.

(3) if a sorted multi-set is the right data structure, is there a Scala implementation of this? Or a binary-sorted Vector or Array, if the result set is reasonably small?

  • 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-26T07:44:55+00:00Added an answer on May 26, 2026 at 7:44 am

    You can use a priority queue:

    def firstK[A](xs: Seq[A], k: Int)(implicit ord: Ordering[A]) = {
      val q = new scala.collection.mutable.PriorityQueue[A]()(ord.reverse)
      val (before, after) = xs.splitAt(k)
      q ++= before
      after.foreach(x => q += ord.max(x, q.dequeue))
      q.dequeueAll
    }
    

    We fill the queue with the first k elements and then compare each additional element to the head of the queue, swapping as necessary. This works as expected and retains duplicates:

    scala> firstK(Vector(9, 2, 6, 1, 7, 5, 2, 6, 9), 4)
    res14: scala.collection.mutable.Buffer[Int] = ArrayBuffer(6, 7, 9, 9)
    

    And it doesn’t sort the complete list. I’ve got an Ordering in this implementation, but adapting it to use an evaluation function would be pretty trivial.

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

Sidebar

Related Questions

Let's say I have a large collection of Image models and at any one
Let's say I have a collection of Person objects, each of which looks like
How do I save a large collection with NHibernate which has elements that surpass
Given a collection of say, 50, images with various widths and heights, how would
Given a large legacy project utilizing ASP.NET, javascript, css, etc, technologies, I was wondering
Given a large text block from a WYSIWYG like: Lorem ipsum dolor sit amet,
Given a large table (10-100 million rows) what's the best way to add some
Suppose I'm given a large dictionary in flat file with 200 million words and
I have been given a large quantity of Xml's where I need to pull
Assume you are given a large number of variables that need to be initialized

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.