Java中如何在main方法调用OutputCountsAsCSV并传递统计结果
问题:词频统计结果写入CSV时的参数不匹配问题
我尝试加载JSON文件并转换为ConcurrentHashMap,再将词频统计结果写入CSV文件,编写了DescriptiveStatistics类代码。现在需要在main(String[] args)方法中调用OutputCountsAsCSV方法,传入CountWordsInCorpus返回的词频统计ConcurrentHashMap和文件名'my_file.csv',但调用时出现参数不匹配错误,请问该如何正确实现?
JSON文件格式示例
{"lemmas":{"doc4":"which might make it go wrong","doc3":"and no dirty datum","doc2":"each of vary length","doc1":"you should find that it have five line","doc0":"this be a simple text file"}}
原实现代码
package pipeline; import java.io.FileWriter; import java.util.ArrayList; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import helpers.JSONIOHelper; public class DescriptiveStatistics { private static void StartCreatingStatistics(String filePath) { System.out.println("Loading file..."); JSONIOHelper JSONIO = new JSONIOHelper(); JSONIO.LoadJSON(filePath); ConcurrentHashMap<String, String> lemmas = JSONIO.GetLemmasFromJSONStructure(); lemmas.forEach((k, v) -> System.out.printf(" %s%n", v)); CountWordsInCorpus(lemmas); } private static ConcurrentHashMap<String, Integer> CountWordsInCorpus(ConcurrentHashMap<String, String> lemmas) { ArrayList<String> corpus = new ArrayList<String>(); 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); } } return counts; } 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; System.out.println(CSVOutput); try (FileWriter writer = new FileWriter(filename)) { writer.write(CSVOutput); System.out.println("CSV File saved successfully..."); } catch (Exception e) { System.out.println("Saving CSV to file failed..."); } } } }
错误的main方法尝试
public static void main(String[] args) { String filePath = "JSON_simple.json"; DescriptiveStatistics newobj = new DescriptiveStatistics(); newobj.StartCreatingStatistics(filePath); String filename = "my_file.csv"; // 尝试获取CountWordsInCorpus返回值时出现参数不匹配错误 // ConcurrentHashMap<String, Integer> newhashmap = newobj.CountWordsInCorpus() OutputCountsAsCSV(newhashmap, filename); }
问题分析与修复方案
1. 静态方法与实例方法调用混淆
CountWordsInCorpus是静态方法,不能通过实例对象newobj调用,直接用类名调用即可;同时它需要传入ConcurrentHashMap<String, String>类型的参数。OutputCountsAsCSV是实例方法,必须通过类的实例对象调用,或者改为静态方法以便在main中直接调用。
2. 方法参数缺失
原main方法中调用CountWordsInCorpus时没有传入必要的lemmas参数,导致参数不匹配错误。
3. CSV写入逻辑优化
原OutputCountsAsCSV方法在循环内重复打开/关闭文件,会导致文件被多次覆盖且效率低下,应该将文件写入操作放在循环外部。
修复后的完整代码
调整后的DescriptiveStatistics类
package pipeline; import java.io.FileWriter; import java.util.ArrayList; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import helpers.JSONIOHelper; public class DescriptiveStatistics { // 修改方法,返回统计结果以便后续使用 private static ConcurrentHashMap<String, Integer> StartCreatingStatistics(String filePath) { System.out.println("Loading file..."); JSONIOHelper JSONIO = new JSONIOHelper(); JSONIO.LoadJSON(filePath); ConcurrentHashMap<String, String> lemmas = JSONIO.GetLemmasFromJSONStructure(); lemmas.forEach((k, v) -> System.out.printf(" %s%n", v)); // 直接返回统计结果 return CountWordsInCorpus(lemmas); } private static ConcurrentHashMap<String, Integer> CountWordsInCorpus(ConcurrentHashMap<String, String> lemmas) { ArrayList<String> corpus = new ArrayList<String>(); 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) { counts.put(word, counts.getOrDefault(word, 0) + 1); } return counts; } // 修改为静态方法,方便在main中直接调用 private static void OutputCountsAsCSV(ConcurrentHashMap<String, Integer> counts, String filename) { StringBuilder CSVOutput = new StringBuilder(); // 用StringBuilder提升字符串拼接效率 // 先拼接所有行数据 for (Entry<String, Integer> entry : counts.entrySet()) { String rowText = String.format("%s,%d%n", entry.getKey(), entry.getValue()); System.out.println(rowText); CSVOutput.append(rowText); } // 一次性写入文件 try (FileWriter writer = new FileWriter(filename)) { writer.write(CSVOutput.toString()); System.out.println("CSV File saved successfully..."); } catch (Exception e) { System.out.println("Saving CSV to file failed..."); e.printStackTrace(); // 打印异常信息便于排查问题 } } // 正确的main方法 public static void main(String[] args) { String filePath = "JSON_simple.json"; // 获取词频统计结果 ConcurrentHashMap<String, Integer> wordCounts = StartCreatingStatistics(filePath); String filename = "my_file.csv"; // 调用静态方法写入CSV OutputCountsAsCSV(wordCounts, filename); } }
内容的提问来源于stack exchange,提问作者Bluetail
相关产品推荐
相关产品推荐

