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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:55:30+00:00 2026-05-20T23:55:30+00:00

I have an unsorted Collection of objects [that are comparable], is it possible to

  • 0

I have an unsorted Collection of objects [that are comparable], is it possible to get a sub list of the collection of the list without having to call sort?

I was looking at the possibility of doing a SortedList with a limited capacity, but that didn’t look like the right option.

I could easily write this, but I was wondering if there was another way.

I am not able to modify the existing collection’s structure.

  • 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-20T23:55:31+00:00Added an answer on May 20, 2026 at 11:55 pm

    Since you don’t want to call sort(), it seems like you are trying to avoid an O(n log(n)) runtime cost. There is actually a way to do that in O(n) time — you can use a selection algorithm.

    There are methods to do this in the Guava libraries (Google’s core Java libraries); look in Ordering and check out:

    • public <E extends T> List<E> Ordering.leastOf(Iterable iterable, int k)
    • public <E extends T> List<E> Ordering.greatestOf(Iterable iterable, int k)

    These are implementations of quickselect, and since they’re written generically, you could just call them on your Set and get a list of the k smallest things. If you don’t want to use the entire Guava libraries, the docs link to the source code, and I think it should be straightforward to port the methods to your project.

    If you don’t want to deviate too far from the standard libraries, you can always use a sorted set like TreeSet, though this gets you logarithmic insert/remove time instead of the nice O(1) performance of the hash-based Set, and it ends up being O(n log(n)) in the end. Others have mentioned using heaps. This will also get you O(n log(n)) running time, unless you use some of the fancier heap variants. There’s a fibonacci heap implementation in GraphMaker if you’re looking for one of those.

    Which of these makes sense really depends on your project, but I think that covers most of the options.

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

Sidebar

Related Questions

I have a list of news unsorted items, some of which have a priority
We have two unsorted arrays and each array has a length of n. These
I have an array: array([[ 4, 10], [ 4, 2], [ 0, 7], [
There have been various similar questions, but they either referred to a too specific
So I hava an unsorted array of 30 ints like 340, 6401 1280, and
I have a database, which I query, and I'm unsure of where to perform
So I have an input file. It consists of 40 numbers. The first 20
I went to an interview today and was asked this question: Suppose you have
What's the best way to simulate a troolean variable with an HTML form? Specifically,
I'm going through Intermediate Perl and it's pretty cool. I just finished the section

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.