import java.util.HashMap; import java.util.List; import java.util.Map; public class TitleLengthAnalyzer implements ArticleAnalyzer { @Override public Map analyze(List
articles) { Map result = new HashMap<>(); if (articles.isEmpty()) { result.put("analyzer", getName()); result.put("averageLength", 0); result.put("maxLength", 0); result.put("minLength", 0); return result; } int totalLength = articles.stream() .mapToInt(a -> a.getTitle().length()) .sum(); int maxLength = articles.stream() .mapToInt(a -> a.getTitle().length()) .max() .orElse(0); int minLength = articles.stream() .mapToInt(a -> a.getTitle().length()) .min() .orElse(0); result.put("analyzer", getName()); result.put("averageLength", (double) totalLength / articles.size()); result.put("maxLength", maxLength); result.put("minLength", minLength); return result; } @Override public String getName() { return "标题长度分析器"; } }