[英]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. 现在,我正在尝试使用我的这种方法来打印HashMap的.txt文件,该文件包含一个单词作为Key,以及它在读取的.txt文件中出现的次数(通过另一种方法完成)一个值。 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.
该方法需要按字母顺序放置HashMap键,然后在单独的.txt文件中在其旁边打印相应的Value。
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. 我的问题是,虽然打印了.txt文件,并且单词以完美的ASCII顺序(我需要),但单词旁边的每个值都返回null。 I've tried many different ways of fixing this but to no avail.
我尝试了许多不同的方法来解决此问题,但无济于事。 I think the problem is in my 'for' loop:
我认为问题出在我的“ for”循环中:
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). 如果需要所有项,也从索引0开始(请参见上面循环中的初始条件)。 As suggested in a comment, you can alternatively use a TreeMap to iterate over the keys of the map in order
如评论中所建议,您也可以使用TreeMap依次遍历地图的键
This is where an enhanced for loop would have kept you from making a mistake. 这是增强的for循环可以防止您出错的地方。 You get values from a
Map
by using get(key)
: 您可以使用
get(key)
从Map
获取值:
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: 并且您可以使用TreeSet进行排序,从而进一步简化事情:
for ( final String key : new TreeSet<String>( vocab.keySet() ) ) {
out.println( key + " " + vocab.get( key ) );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.