我有一个类可以计算在java中使用Lambdas的语句的平均单词数。我遇到的问题是,如果corp为null或为空,我需要返回0.目前,我得到NaN,如果corp为null或为空。我的代码的其余部分做它应该做的,但我无法弄清楚这一部分。使用lambdas检查是否等于null或空
public class AverageNumberOfWordsPerSentence extends TextMetric<Double> {
@Override
public Double apply(final Corpus corp) {
Sentences sentences = new Sentences();
List<String> sentenceList = sentences.apply(corp);
LongSummaryStatistics lss = corp.texts().stream()
.map(blob -> blob.text())
.flatMap(string -> stream
(string.split("\\W+")))
.filter(string -> !string.isEmpty())
.mapToLong(String::length)
.summaryStatistics();
return (double)lss.getCount()/
sentenceList.size();
}
为什么不老式的方式,明确地检查它? (虽然,坦率地说:http://stackoverflow.com/a/6921270/869736) –
'corp == null'是最好也是最简单的方法,你什么意思是空'corp'? – 11thdimension
我不允许使用控制结构。这就是为什么这是困难的,我会张贴公司很快。 – ObiJuanKanobe