将 Java 8 Optional 用于字符串列表作为输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30095651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Java 8 Optional for List of String as output
提问by Scorpion
I want to use Optional for a method which returns a List
我想对返回列表的方法使用 Optional
Lets say the function is
让我们说这个功能是
public Output getListOfSomething() {
// In some cases there is nothing to return and hence it makes sense to have return
// type as Optional here
}
Hence the function looks like :
因此函数看起来像:
public Optional<List<String>> getListOfSomething() {
// return something only when there is some valid list
}
Now I want to do something if the list is present so something like :
现在,如果列表存在,我想做一些事情,例如:
Optional<List<String>> listOfSomething = getListOfSomething();
int size = 0;
listOfSomething.ifPresent(size = listOfSomething.get().size());
I am new to Optional and have gone through the articles about Optional and it seems like this should work however am getting syntax error in my IDE :
我是 Optional 的新手,已经阅读了有关 Optional 的文章,看起来这应该可行,但是我的 IDE 中出现语法错误:
method ifPresent is not applicable for the arguments (void).
方法 ifPresent 不适用于参数 (void)。
I wanted to get some help from developers who might be more fluent with lamdas in java 8.
我想从可能更熟悉 java 8 中的 lamdas 的开发人员那里获得一些帮助。
回答by Tagir Valeev
The true functional-programming way is the following:
真正的函数式编程方式如下:
size = listOfSomething.map(List::size).orElse(0);
But it would be much better to return an empty List
instead of Optional
.
但是返回一个空的List
而不是Optional
.
回答by pbod
ifPresent requires a Consumerinterface to work. You could do the following:
ifPresent 需要消费者接口才能工作。您可以执行以下操作:
Optional<List<String>> listOfSomething = getListOfSomething();
Integer[] size = {0};
listOfSomething.ifPresent(list -> size[0]=list.size())
But as stated by Tagir Valeevit would be better to do:
但正如 Tagir Valeev 所说,最好这样做:
size = listOfSomething.map(List::size).orElse(0);
And it would also be better to return an empty List or even a Stream maybe.
而且最好返回一个空的 List 或者甚至一个 Stream 。
回答by Daniel
It's important to think about the Semantics here.
在这里考虑语义很重要。
Your method could return a List, or "no list".
您的方法可以返回一个列表,或“无列表”。
If it returns a List, it could return an Empty list.
如果它返回一个列表,它可以返回一个空列表。
You should ask, "is there a semantic reason to distinguish between an Empty List, and No List?" Sometimes there is a good design reason to make the difference, but it is rare. Think long and hard before deciding that Empty and Null are different in your case. Part of the reason to avoid No List, is that it reduces "special cases" that the client code has to consider. For example, if they have to do something for every item returned, but you could also return null, they have to do a special check for null before going into a for each loop. A for each does nothing if the list is empty.
您应该问,“是否有语义上的原因来区分空列表和无列表?” 有时有一个很好的设计理由来产生差异,但这种情况很少见。在决定 Empty 和 Null 在您的情况下是不同的之前,请深思熟虑。避免使用 No List 的部分原因是它减少了客户端代码必须考虑的“特殊情况”。例如,如果他们必须对返回的每个项目执行某些操作,但您也可以返回 null,则他们必须在进入 for each 循环之前对 null 进行特殊检查。如果列表为空,则 A for each 不执行任何操作。
If a "No List" is distinct from an "Empty List" in your problem domain, then it is sometimes useful to return wrapper class that helps client code distinguish between those conditions, and handle them appropriately. Optional
is one such generic class, but your domain may call for something more specific (even if it mimics the functionality of Optional, it might have better semantic definition).
如果“无列表”与问题域中的“空列表”不同,那么有时返回包装类有助于客户端代码区分这些条件并适当处理它们。Optional
是一个这样的通用类,但你的领域可能需要更具体的东西(即使它模仿了 Optional 的功能,它可能有更好的语义定义)。