英文:
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<List<String>> removeShortWords(Optional<List<String>> 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<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;
}
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<List<String>> removeShortWords(Optional<List<String>> maybeWords) {
return maybeWords.map(list ->
list.stream().filter(s -> s.length() >= 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<List<String>> removeShortWords(Optional<List<String>> maybeWords) {
return maybeWords.map(x -> x.stream().filter(y -> y.length() >= 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<String>
, since List<String>
is already nullable. And check for null "normally". See here for more info.
答案3
得分: 1
你可以使用 Optional#ifPresent
和 Collection#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<List<String>> removeShortWords(Optional<List<String>> maybeWords) {
maybeWords.ifPresent(list -> list.removeIf(str -> str.length() < 5));
return maybeWords;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论