How to get words average length using Lambda Expression

后端 未结 3 1357
终归单人心
终归单人心 2021-01-04 17:24

I have a word list text file, I want to get min, max and average word lengths from that file.

I have a stream method:

public static Stream

        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 17:38

    Use IntSummaryStatistics to get the min, max and average in one pass.

    IntSummaryStatistics summary = readWords(filename)
        .collect(Collectors.summarizingInt(String::length));
    System.out.format("min = %d, max = %d, average = %.2f%n",
        summary.getMin(), summary.getMax(), summary.getAverage());
    

提交回复
热议问题