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"]
desired output = "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.
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?
nulllist is “null”. Do you mean the string"null"or anullreference? Using an empty stream as fallback serves neither…