【发布时间】:2022-08-10 00:30:53
【问题描述】:
我正在尝试将 ConcurrentHashMap 写入 csv 文件,并收到此错误:
Exception in thread \"main\" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method OutputCountsAsCSV(ConcurrentHashMap<String,Integer>, String) from the type BDescriptiveStatistics
我有一个这样的json文件
{\"lemmas\":{\"doc4\":\"这可能会出错\",\"doc3\":\"并且没有脏数据\",\"doc2\":\"每个改变长度\",\"doc1\":\"你应该会发现它有五行\",\"doc0\":\"这是一个简单的文本文件\"}}
我希望 csv 文件看起来像这样:
datum,1
no,1
be,1
vary,1
...
it,2
所以我在我的主要方法中有这个问题 - 我不知道如何调用方法,私有静态 ConcurrentHashMap<String, Integer>CountWordsInCorpus(..) 它返回计数并将其用作 OutputCountsAsCSV() 写入的第一个参数一个csv文件。
这是我尝试过的。
public static void main(String[] args) {
StartCreatingStatistics(\"JSONDataStore_simple.json\");
// BDescriptiveStatistics newobj = new BDescriptiveStatistics();
// newhashmap = newobj.CountWordsInCorpus(ConcurrentHashMap<String, String> lemmas);
ConcurrentHashMap<String, Integer> newhashmap = new ConcurrentHashMap<String, Integer>();
OutputCountsAsCSV ( newhashmap, \"csv_file.csv\");
}
我知道我需要改为启动 BDescriptiveStatistics 类的对象。 如果我运行两个未注释的行,我会得到一个不同的错误,\'newhashmap 无法解析为变量\'。
我的其余代码如下:
package pipeline;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import helpers.JSONIOHelper;
public class BDescriptiveStatistics {
private static void StartCreatingStatistics(String filePath) {
System.out.println(\"Loading file...\");
JSONIOHelper JSONIO = new JSONIOHelper(); // create an object of the JSONIOHelper class
JSONIO.LoadJSON(filePath); // call the LoadJSON method
ConcurrentHashMap<String, String> lemmas = JSONIO.GetLemmasFromJSONStructure();
lemmas.forEach((k, v) -> System.out.printf(\" %s%n\", v));
CountWordsInCorpus(lemmas);
}
// This method compiles the words and the frequency of each word and returns counts as a hash map.
private static ConcurrentHashMap<String, Integer> CountWordsInCorpus(ConcurrentHashMap<String, String> lemmas) {
// compile the words in the corpus into a list
ArrayList<String> corpus = new ArrayList<String>();
// store the words together with their frequencies
ConcurrentHashMap<String, Integer> counts = new ConcurrentHashMap<String, Integer>();
for (Entry<String, String> entry : lemmas.entrySet()) {
for (String word : entry.getValue().split(\" \")) {
corpus.add(word);
}
}
for (String word : corpus) {
if (counts.containsKey(word)) {
counts.put(word, counts.get(word) + 1);
} else {
counts.put(word, 1);
}
}
for (String word : counts.keySet()) {
String wordKey = word;
Integer countsValue = counts.get(word);
System.out.println(wordKey + \" | \" + countsValue);
}
return counts; // its a ConcurrentHashMap<String, Integer>
}
private void OutputCountsAsCSV(ConcurrentHashMap<String, Integer> counts, String filename) {
String CSVOutput = new String(\"\");
for (Entry<String, Integer> entry : counts.entrySet()) {
String rowText = String.format(\"%s,%d\\n\", entry.getKey(),entry.getValue());
System.out.println(rowText);
CSVOutput += rowText;
}
}
谢谢你。
标签: java csv concurrenthashmap