[英]Using Java 8 Stream to replace for loop and populate a Map
In this java assignment we have a for loop that reads through a text file we use in this program, and we are supposed to replace it with a stream. 在此Java分配中,我们有一个for循环,该循环读取用于该程序的文本文件,并应将其替换为流。 Here is part of the program and what we are supposed to replace: 这是程序的一部分,应该替换掉:
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);
}
}
}
I have tried this and I can't seem to figure out anything else: 我已经尝试过了,但似乎无法解决其他问题:
incoming.stream()
.collect(Collectors.toMap(word -> word, word -> 1, Integer::sum)).entrySet();
Here's what you can try: 您可以尝试以下方法:
wordFrequency = incoming.stream()
.map(String::toLowerCase).filter(word -> !word.trim().isEmpty())
.collect(Collectors.toMap
(word -> word, word -> 1, (a, b) -> a + b, TreeMap::new));
You missed the BinaryOperator
that will merge values of the key
already exists Collectors.toMap()
您错过了BinaryOperator
,它将合并已存在的key
值Collectors.toMap()
As an alternative, you can simply use : 或者,您可以简单地使用:
wordFrequency = incoming.stream()
.map(String::toLowerCase) // only if you're planning to store in lowercase or else move this to filtering predicate
.filter(word -> !word.trim().isEmpty())
.collect(Collectors.toMap(Function.identity(),
word -> 1, Integer::sum));
considering that you're aware that entrySet()
is a Set
instead which cannot be assigned to a Map
anyway as in the question. 考虑到您知道entrySet()
是一个Set
,因此无论如何都不能将其分配给Map
。
private static Map<String,Integer> toMapFunction(Collection< ? extends String> collection){
return collection.stream().map(String::toLowerCase)
.filter(str -> !str.trim().isEmpty())
.collect(Collectors.toMap(Function.identity(), value -> 1, (oldValue, newValue) -> oldValue + newValue, TreeMap::new));
}
public static void main(String[] args) {
List<String> stringList = Arrays.asList("joy", "joy", "lobo", "lobo", "lobo", "joy", "joy", "joy", "david", "hibbs");
System.out.println(toMapFunction(stringList));
}
and this will be output of the program: 这将是程序的输出:
{david=1, hibbs=1, joy=5, lobo=3}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.