简体   繁体   中英

Printing a HashMap into a .txt file

Right now, I'm trying to get this method of mine to print a .txt file of a HashMap that contains a word as a Key, and the number of times it appears in a read .txt file (done in another method) as a Value. The method needs to put the HashMap Keys in alphabetical order, and then print the corresponding Value next to it in a separate .txt file.

Here is my code for the method:

  public static void writeVocabulary(HashMap<String, Integer> vocab, String fileName) {

    // Converts the given HashMap keys (the words) into a List.
    // Collections.sort() will sort the List of HashMap keys into alphabetical order.
    List<String> listVal = new ArrayList<String>(vocab.keySet()); 
    Collections.sort(listVal);


    try 
    {
      // Creating the writer
      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); 

      for (int i = 1; i < listVal.size(); i++) {
        out.println(listVal.get(i) + " " + vocab.get(i));
      }

      out.close();
    }
    // Catching the file not found error
    // and any other errors
    catch (FileNotFoundException e) {
      System.err.println(fileName + "cannot be found.");
    }
    catch (Exception e) {
      System.err.println(e);
    }
  }

My problem is that, while a .txt file is printed, and the words are in perfect ASCII order (what I need), every value next to the word is returned null. I've tried many different ways of fixing this but to no avail. I think the problem is in my 'for' loop:

   for (int i = 1; i < listVal.size(); i++) {
    out.println(listVal.get(i) + " " + vocab.get(i));
      }

I'm pretty sure my logic in this is faulty but I can't think of a solution. Any help would be much appreciated. Thanks in advance!

You need to use the correct map key to get the value from the map - this code currently uses the index in the List, not the value in the list (which is the actual key to the map).

for (int i = 0; i < listVal.size(); i++) {
    out.println(listVal.get(i) + " " + vocab.get(listVal.get(i)));
}

And also start at index 0 if you want all the items (see initial condition in loop above). As suggested in a comment, you can alternatively use a TreeMap to iterate over the keys of the map in order

This is where an enhanced for loop would have kept you from making a mistake. You get values from a Map by using get(key) :

for ( String key : listVal ) {
    out.println( key + " " + vocab.get(key) );
}

You don't need to iterate through the list using the indexes; instead you can use:

for ( final String key : listVal ) {
    out.println( key + " " + vocab.get( key ) );
}

and you can simplify things even further using a TreeSet to do the sorting:

for ( final String key : new TreeSet<String>( vocab.keySet() ) ) {
    out.println( key + " " + vocab.get( key ) );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM