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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:35:33+00:00 2026-05-30T01:35:33+00:00

I have been reading up on maps and understand some of the differences in

  • 0

I have been reading up on maps and understand some of the differences in tree maps and hash, sorted maps. I was trying to get a map to be sorted when outputting it.

What I needed to be able to do was:

  1. Take a text file and read in the content.
  2. Break it into separate words. Use the words as the key and the value as how many times the key occurs in the txt file.
  3. If the word is at the end of a sentence I am to make it a separate key. E.g., my and my. are two separate keys.

My problem is that no matter if I declare it as a tree, hash or sorted map, I can’t get it to output/iterate through in an ordered way. I wanted it to output with the highest occurring value first, but I can’t even get it to output with the key in any order.

public static Map<String, Integer> createDictionary(String _filename)
{
    TreeMap<String, Integer> dictionary = new TreeMap<String, Integer>(); // Changed Hash to _______

    try {
          FileReader myFileReader=new FileReader(_filename); // File reader stream open
          BufferedReader myBuffReader=new BufferedReader(myFileReader);

          String str = "\0";

          while (str != null) { // While there are still strings in the file
              str = myBuffReader.readLine(); // We read a line into the str variable

              if (str != null) { // Make sure its not the last line/EOF 
                  // System.out.println(str); // Used for testing. 
                  StringTokenizer myTokenStr=new StringTokenizer(str," \t"); // Create a StringToken obj from the string

                  while (myTokenStr.hasMoreTokens()) {
                      String tokStr = myTokenStr.nextToken(); // Each token is put into an individual string
                      // System.out.println(tokStr);

                      if (dictionary.containsKey(tokStr)) {
                          int value = dictionary.get(tokStr); // Add one to the integer value
                          // dictionary.remove(tokStr); // Was doing this way but just using put method works 
                          // dictionary.put(tokStr, value + 1);
                          dictionary.put(tokStr, value + 1);
                      }
                      else {
                          dictionary.put(tokStr, 1); // Add the string as the key with an int value of one for the value
                      }
                  }
              }
          }

          myBuffReader.close(); // Close stream
          myFileReader.close(); // Close stream
      }
      catch (FileNotFoundException e) {
          System.out.println("File Not Found");
      }
      catch (IOException e) { }

      // System.out.println(dictionary.entrySet());

      return dictionary;
}
  • 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-30T01:35:35+00:00Added an answer on May 30, 2026 at 1:35 am

    Your map is sorted alphabetically, not by number of occurrences. You need to postprocess the map after the initial parsing. I would suggest:

    1. Parse file into HashMap<String, Integer>
    2. Iterate through HashMap, and add elements to a TreeMap<Integer, Set<String> > (see below).
    3. Output the TreeMap.

    You can achieve step 2. by something like:

    TreeMap<Integer, Set<String> > treeMap = new TreeMap<Integer, Set<String> > ();
    for (Map.Entry<String, Integer> entry: hashMap) {
        Set<String> set = treeMap.get(entry.value());
        if (set == null) {
            set = new TreeSet<String>();
            treeMap.put(entry.value(), set);
        }
        set.add(entry.key());
    }
    

    Using TreeSet here sorts the words with same number of occurrences alphabetically, you could use any other Set or List though.

    For descending order in step 3.:

    for (Map.Entry<Integer, Set<String> > entry: treeMap.descendingMap())
        for (String word: entry.getValue())
            System.out.println(String.format("%d: %s", entry.getKey(), word));
    

    That should do it.

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

Sidebar

Related Questions

I'm trying to improve my Javascript coding style and have been reading that it's
I have been reading up on pickers and wanted to try and get a
I have been reading several tutorials and watching some iTube videos to see how
I have acquired Digital Elevation Maps(Height Map of Earth) of some area. My aim
I have been reading about websockets and also about socket.io. I understand websockets are
I have been reading the web and trying out things for days looking for
I have been reading this article to understand the gcroot template. I understand the
I have been reading a number of tutorials on implementing Structure Map IoC/di into
I have been reading about the differences between Table Variables and Temp Tables and
I have been reading this similar SO question but some of the approaches suggested

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.