我正在进行一项使用带有lambda语法的Java流的作业。该程序应该被设计(1)计算一组文件(2)计算这些文件中的单词(3)打印并显示结果。这是输出的一个例子:
Count 11 files:
word length: 1 ==> 80
word length: 2 ==> 321
word length: 3 ==> 643
.....
但是,我得到的是这个输出:
primes.txt
word length: 1 ==> hw8.WordCount@5c647e05
constitution.txt
word length: 2 ==> hw8.WordCount@33909752
short.txt
word length: 3 ==> hw8.WordCount@55f96302
.....
Count: 11 files
我写的程序分为两个类 - FileCatch
,它计算文件,WordCount
计算单词(理论上)。如果有人有任何编程技巧可以提供帮助,我将不胜感激。
FileCatch
班
public class FileCatch8 {
public static void main(String args[]) {
List<String> fileNames = new ArrayList<>();
try {
DirectoryStream<Path> directoryStream = Files.newDirectoryStream
(Paths.get("files"));
int fileCounter = 0;
for (Path path : directoryStream) {
System.out.println(path.getFileName());
fileCounter++;
fileNames.add(path.getFileName().toString());
WordCount WordCnt = new WordCount();
System.out.println("word length: " + fileCounter + " ==> " + WordCnt);
}
}catch(IOException ex){
}
System.out.println("Count: "+fileNames.size()+ " files");
}
}
WordCount
类:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.AbstractMap.SimpleEntry;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import java.util.stream.Stream;
/**
*
* @author GeraldShields
*/
public class WordCount {
/**
*
* @return
* @throws IOException
*/
public Map<String, Long> WordCount()throws IOException {
Stream<String> lines = Files.lines(Paths.get("constitution.txt"));
Map<String, Long> wordMap = lines
.parallel()
.map(String::toLowerCase)
.map(line -> line.split("\\W+"))
.flatMap(line -> Arrays.asList(line).stream())
.filter(word -> !word.matches("\\d+") && word.trim().length() != 0)
.map(word -> new SimpleEntry<>(word, 1))
.collect(groupingBy(SimpleEntry::getKey, counting()));
new TreeMap(wordMap).forEach((k, v) ->
System.out.println(String.format("%s word length: 1 ==> %d", k, v)));
return wordMap;
}
}
答案 0 :(得分:0)
您正在打印WordCount
课程的实例,这没有任何意义。您也永远不会调用WordCount()
方法。
您应该创建WordCount
的单个实例,并调用为每个文件执行单词计数的方法(我重命名了一些方法和变量以使代码更具可读性):
WordCount wordCnt = new WordCount();
for (Path path : directoryStream) {
System.out.println(path.getFileName());
fileCounter++;
fileNames.add(path.getFileName().toString());
System.out.println("word length: " + fileCounter + " ==> " + wordCnt.count(path.getFileName().toString()));
}
public class WordCount {
/**
*
* @return
* @throws IOException
*/
public Map<String, Long> count(String filename) throws IOException {
Stream<String> lines = Files.lines(Paths.get(filename));
Map<String, Long> wordMap = lines
.parallel()
.map(String::toLowerCase)
.map(line -> line.split("\\W+"))
.flatMap(line -> Arrays.asList(line).stream())
.filter(word -> !word.matches("\\d+") && word.trim().length() != 0)
.map(word -> new SimpleEntry<>(word, 1))
.collect(groupingBy(SimpleEntry::getKey, counting()));
new TreeMap(wordMap).forEach((k, v) ->
System.out.println(String.format("%s word length: 1 ==> %d", k, v)));
return wordMap;
}
}
请注意,您的WordCount
类似乎计算文件中每个单词的外观,而不是文件中单词的总数,因此它似乎与您在开始时发布的预期输出不匹配你的问题。