使用Java 8 Stream替换for循环并填充Map

时间:2018-10-16 03:01:47

标签: java file loops filter java-stream

在此java分配中,我们有一个for循环,该循环读取用于此程序的文本文件,并应将其替换为流。这是程序的一部分,应该替换:

    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();

3 个答案:

答案 0 :(得分:5)

您可以尝试以下操作:

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,它将合并Collectors.toMap()已经存在的key的值

答案 1 :(得分:0)

作为替代,您可以简单地使用:

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));

考虑到您知道entrySet()Set而不是像问题中一样无法分配给Map的情况。

答案 2 :(得分:0)

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));
    }

这将是程序的输出:

{david=1, hibbs=1, joy=5, lobo=3}