[英]Java stream to collection for nullable string list
I have a list that may be null.我有一个可能为空的列表。 I would like to concatenate the strings in that list into one string.
我想将该列表中的字符串连接成一个字符串。
eg.例如。 A
一
list = ["hello","there"] list = ["你好","那里"]
desired output = "hello.there."所需的输出 = "hello.there."
eg.例如。 B
乙
list = null列表 = 空
desired output = null所需的输出 = 空
I have a method that works for non-null lists:我有一个适用于非空列表的方法:
List<String> myStringList = foo.getlist();
String result = myStringList.stream()
.collect(Collectors.joining(".","","."));
but the stream will throw an error if myStringList is null.但如果 myStringList 为空,流将抛出错误。
I believe I should be using an optional stream similar to:我相信我应该使用类似于以下内容的可选流:
List<String> myStringList = foo.getlist();
String result = Optional.ofNullable(myStringList)
.map(Arrays::stream)
.orElseGet(Stream::empty)
.collect(Collectors.joining(".","","."))
However when I try this i get errors like "can not resolve method 'stream'".但是,当我尝试此操作时,会出现“无法解析方法‘流’”之类的错误。
Am I going about this the right way?我会以正确的方式解决这个问题吗? Why am I getting errors for the code?
为什么我收到代码错误?
This is a personal opinion, but I think you shouldn't use Optional
that way.这是个人意见,但我认为您不应该那样使用
Optional
。 Optional
is not intended to replace a simple null-check. Optional
并不打算取代简单的空检查。 If you already have the Optional
, then use it, or use it as the possibly absent return value of a method.如果您已经拥有
Optional
,则使用它,或者将其用作方法的可能不存在的返回值。 Otherwise, I think it's more expressive and readable to show your exact intention wrt the list being null
or not:否则,我认为在列表是否为
null
显示您的确切意图更具表现力和可读性:
List<String> myStringList = foo.getlist();
String result = myStringList == null ?
null :
myStringList.stream().collect(Collectors.joining(".","","."));
In this cases, the ternary operator does a nice work.在这种情况下,三元运算符做得很好。
EDIT:编辑:
If the joined string was the return value of a method, then using Optional
and returning it would be a perfect fit:如果连接的字符串是方法的返回值,那么使用
Optional
并返回它将是一个完美的选择:
private Optional<String> joinedFooList(Foo foo) {
List<String> myStringList = foo.getlist();
return Optional.ofNullable(myStringList)
.map(l -> l.stream().collect(Collectors.joining(".","",".")));
}
Then, you could use the returned Optional
:然后,您可以使用返回的
Optional
:
joinedFooList(someFooInstance)
.ifPresent(joined -> /* so something with the joined string */);
The problem is that you're trying to use Arrays::stream
to convert your List
to a stream.问题是您正在尝试使用
Arrays::stream
将您的List
转换为流。 But, Arrays::stream
is for arrays .但是,
Arrays::stream
是用于arrays 的。
You need List::stream
(which is the inherited version of Collection::stream
, so that one would work too):您需要
List::stream
(它是Collection::stream
的继承版本,因此也可以使用):
String result = Optional.ofNullable(myStringList)
.map(List::stream)
.orElseGet(Stream::empty)
.collect(Collectors.joining(".","","."));
you can use Collections.emptyList()你可以使用Collections.emptyList()
1- List not null : 1- 列表不为空:
List<String> myStringList = new ArrayList<>() ;
myStringList.add("hello");
myStringList.add("hello");
String result = Optional.ofNullable(myStringList).orElse(Collections.emptyList()).stream()
.collect(Collectors.joining(".","","."));
System.out.println(result);
output : hello.hello.
输出:
hello.hello.
2- List null : 2-列表空:
List<String> myStringList2 = null;
result = Optional.ofNullable(myStringList2).orElse(Collections.emptyList()).stream()
.collect(Collectors.joining(".","","."));
System.out.println(result);
output : .
输出:
.
you can edit your output if you want如果需要,您可以编辑输出
If third-party libraries are game, Apache CollectionUtils has a nice utility for making lists null-safe:如果第三方库是游戏,Apache CollectionUtils 有一个很好的实用程序可以使列表空安全:
List<String> myStringList = foo.getlist();
String result = CollectionUtils.emptyIfNull(myStringList).stream()
.collect(Collectors.joining(".","","."));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.