You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

44 lines
1.3 KiB

import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TitleLengthAnalyzer implements ArticleAnalyzer {
@Override
public Map<String, Object> analyze(List<Article> articles) {
Map<String, Object> 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 "标题长度分析器";
}
}