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

The Archive Base Latest Questions

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

I am trying to figure out an optimal solution (in Java) to the following

  • 0

I am trying to figure out an optimal solution (in Java) to the following problem:

In a first pass over some data, I count the number of occurrences of an item. Basically, I create a HashMap from item ID to integer and increment the integer every time I see an occurrence of the item. So basically, I have a Map<Long,Integer> from itemID to count.

Now, what I need from this map is the top n item ids sorted by the count.

Apparently HashMap is not the optimal data structure here. Any ideas?

This is for some data mining stuff I am doing at work, so not a hw problem…

  • 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-20T06:30:02+00:00Added an answer on May 20, 2026 at 6:30 am

    Actually, a HashMap is a reasonable solution here because you have to accumulate the totals. There’s no way you can shortcut that, and no simple way to find the top N items until you know the counts for all items.

    After you have the HashMap, there are several ways to do things. If the data is relatively small, create an array of itemId and count pairs, and sort by count in descending order. Then select the top N items.

    If you have lots of items (in the hundreds of thousands), it’s probably faster to use a min heap after you get the counts, the idea being that you put the first N items into the min heap and then only insert an item if its count is larger than the smallest item in the min heap.

    You could keep things in order by count while you’re going through adding things up, but every time you increment a counter you’ll have to remove the thing from the collection and re-insert it. You’re better off accumulating things in a HashMap where it’s easy to look things up by ID, and then post-process to apply ordering by count.

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

Sidebar

Related Questions

I am trying to figure out how to create an optimal solution for my
In trying to figure out this problem (which is still unsolved and I still
I'm trying to figure out what a Java applet's class file is doing under
Ok, so I'm having a problem trying figure out the problem in my code.
I'm trying to figure out the optimal combination of indexes for the query below.
Trying to figure out why I could not set up Heroku Gem and following
Trying to figure out how to exploit the CSharpCodeProvider to compile string data at
Trying to figure out datetime module and need some help. I've got a string
Trying to figure out an equation to get the current group a page would
I'm trying to figure out how big a certain database would be (it hasn't

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.