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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:05:02+00:00 2026-06-13T14:05:02+00:00

I am working on a simple project that obtains data from an input file,

  • 0

I am working on a simple project that obtains data from an input file, gets what it needs and prints it to a file. I am basically getting word frequency so each key is a string and the value is its frequency in the document. The problem however, is that I need to print out these values to a file in descending order of frequency. After making my hashmap, this is the part of my program that sorts it and writes it to a file.

//Hashmap I create
Map<String, Integer> map = new ConcurrentHashMap<String, Integer>();
int valueMax = -1;
//function to sort hashmap
while (map.isEmpty() == false){
            for (Entry<String, Integer> entry: map.entrySet()){
                if (entry.getValue() > valueMax){
                    max = entry.getKey();
                    System.out.println("max: " + max);
                    valueMax = entry.getValue();
                    System.out.println("value: " + valueMax);
                }
            }
            map.remove(max);
            out.write(max + "\t" + valueMax + "\n");
            System.out.println(max + "\t" + valueMax);  
        }   

When I run this i get:

t 9
t 9
t 9
t 9
t 9
....

so it appears the remove function is not working as it keeps getting the same value. I’m thinking i have an issue with a scope rule or I just don’t understand hashmaps very well.

If anyone knows of a better way to sort a hashmap and print it, I would welcome a suggestion.

thanks

  • 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-13T14:05:03+00:00Added an answer on June 13, 2026 at 2:05 pm

    Your code doesn’t work because on every subsequent iteration, entry.getValue() > valueMax is never true because you don’t reset valueMax on re-entry into the while loop.

    You don’t need to muck around with double-looping over a concurrently accessible map though.

    ConcurrentSkipListMap has a lastKey method that returns the greatest key and doesn’t require iteration over the entire map.

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

Sidebar

Related Questions

I'm working on a project that needs a simple, lightweight event server (i.e. a
I'm working on a university project that requires encrypted data to be sent from
I am working on a simple hobby project that checks when users connect/disconnect from
Working on (what should be) a simple project, taking the input from stdin and
I'm working on a research project that needs to store complex data as entities
I'm currently working on a project that is supposed to represent data collected from
The Project I'm working on a relatively simple iPhone OS project that's navigation controller
I'm working with a very small sample project that can be downloaded from here
I'm working on a simple project for my Data Communications class. I predicted it
I am working on a very simple project to learn Rails better, coming from

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.