user9569944:
このJavaの割り当てでは、我々は、このプログラムで使用するテキストファイルを介して読み込むためのループを持っている、と我々は、ストリームと交換することになっています。ここでは、プログラムの一部であり、私たちは代わることになっているもの:
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class FrequentWords {
public static void main(String[] args) throws FileNotFoundException {
String filename = "SophieSallyJack.txt";
if (args.length == 1) {
filename = args[0];
}
Map<String, Integer> wordFrequency = new TreeMap<>();
List<String> incoming = Utilities.readAFile(filename);
// TODO replace the following loop with a single statement using streams
// that populates wordFrequency
for (String word : incoming) {
word = word.toLowerCase();
if (!"".equals(word.trim())) {
Integer cnt = wordFrequency.get(word);
if (cnt == null) {
wordFrequency.put(word, 1);
} else {
int icnt = cnt + 1;
wordFrequency.put(word, icnt);
}
}
}
私はこれを試してみましたし、私は何かを把握するように見えることはできません。
incoming.stream()
.collect(Collectors.toMap(word -> word, word -> 1, Integer::sum)).entrySet();
Shubhendu Pramanik:
ここにあなたが試すことができるものです。
wordFrequency = incoming.stream()
.map(String::toLowerCase).filter(word -> !word.trim().isEmpty())
.collect(Collectors.toMap
(word -> word, word -> 1, (a, b) -> a + b, TreeMap::new));
あなたは逃したBinaryOperator
のマージされます値はkey
すでに存在しているがCollectors.toMap()