过滤一个可选字符串列表

huangapple go评论72阅读模式
英文:

filter a list of optional strings

问题

给定一个方法例如

public Optional<List<String>> removeShortWords(Optional<List<String>> maybeWords) {

}

有没有一种方法可以过滤掉所有长度少于5个字符的字符串
一种方法是先解包 `Optional`,过滤掉短词然后将结果封装在一个新的 `Optional`

public Optional<List<String>> removeShortWords(Optional<List<String>> maybeWords) {

    if (maybeWords.isPresent()) {
        List<String> words = maybeWords.get();

        List<String> longWords = words.stream()
            .filter(word -> word.length() >= 5)
            .collect(Collectors.toList());

        return Optional.of(longWords);
    }

    return maybeWords;
}

肯定有比这更简单的解决方案吧
英文:

Given a method such as

public Optional&lt;List&lt;String&gt;&gt; removeShortWords(Optional&lt;List&lt;String&gt;&gt; maybeWords) {
    
}

Is there a way to filter out all strings that have fewer than 5 characters?
One way is to unwrap the Optional, filter out the short words, and then wrap the result in a new Optional:

public Optional&lt;List&lt;String&gt;&gt; removeShortWords(Optional&lt;List&lt;String&gt;&gt; maybeWords) {

    if (maybeWords.isPresent()) {
        List&lt;String&gt; words = maybeWords.get();

        List&lt;String&gt; longWords = words.stream()
            .filter(word -&gt; word.length() &gt;= 5)
            .collect(Collectors.toList());

        return Optional.of(longWords);
    }

    return maybeWords;
}

Surely there must be a simpler solution than this?

答案1

得分: 2

Optional提供了诸如Optional#map的方法,允许您在值存在的情况下修改其值。您可以在您的情况下使用它:

public Optional<List<String>> removeShortWords(Optional<List<String>> maybeWords) {
    return maybeWords.map(list -> 
        list.stream().filter(s -> s.length() >= 5).collect(Collectors.toList()));
}
英文:

Optional provides methods, such as Optional#map, that allow you to modify its value if the value is present. You'd be able to use that in your case:

public Optional&lt;List&lt;String&gt;&gt; removeShortWords(Optional&lt;List&lt;String&gt;&gt; maybeWords) {
    return maybeWords.map(list -&gt; 
        list.stream().filter(s -&gt; s.length() &gt;= 5).collect(Collectors.toList()));
}

答案2

得分: 2

使用map

public Optional<List<String>> removeShortWords(Optional<List<String>> maybeWords) {
    return maybeWords.map(x -> x.stream().filter(y -> y.length() >= 5)
        .collect(Collectors.toList()));
}

然而,你需要注意 Optional 是设计用于方法的返回类型,而不是参数。我建议直接接受一个 List<String>,因为 List<String> 已经可以是可为空的。然后正常地检查 null 值。详情请参阅这里

英文:

Use map:

public Optional&lt;List&lt;String&gt;&gt; removeShortWords(Optional&lt;List&lt;String&gt;&gt; maybeWords) {
    return maybeWords.map(x -&gt; x.stream().filter(y -&gt; y.length() &gt;= 5)
        .collect(Collectors.toList()));
}

However, you should note that Optional is designed for method returned types, not for parameters. I would just accept a List&lt;String&gt;, since List&lt;String&gt; is already nullable. And check for null "normally". See here for more info.

答案3

得分: 1

你可以使用 Optional#ifPresentCollection#removeIf

public Optional<List<String>> removeShortWords(Optional<List<String>> maybeWords) {
    maybeWords.ifPresent(list -> list.removeIf(str -> str.length() < 5));
    return maybeWords;
}
英文:

You can use Optional#ifPresent and Collection#removeIf.

public Optional&lt;List&lt;String&gt;&gt; removeShortWords(Optional&lt;List&lt;String&gt;&gt; maybeWords) {
    maybeWords.ifPresent(list -&gt; list.removeIf(str -&gt; str.length() &lt; 5));
    return maybeWords;
}

huangapple
  • 本文由 发表于 2020年7月24日 21:46:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63074878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定