I am trying to do null check for the list of values and change it to empty if the value is null.I am getting null as one of the list of value in x.getSomeCall().The null value is not getting added as empty list in the new list
public class Example{
private List<Test> test;
//setter
//getter
}
public class Test{
private List<Test2> test2;
//setter
//getter
}
public class Test2{
private String name;
//setter
//getter
}
public static void main(String args[]){
Example example=new Example(); example.setTest(test);
List<Test> test=new ArrayList<>();
Test t=new Test();
t.setTest2(test);
Test t1=new Test();
Test t2=new Test();
test.add(t);
test.add(t1);
List<Test2> test=new ArrayList<>();
Test2 t=new Test2();
test.add(t);
test.add(null); // I want to get these value as empty list along with the 1st Value in a new list
//Imperative Programming
for(Example ex:example.getTest()){
System.out.println(ex.getTest2());/It prints t object and a null vale
}
When I tried the same with reactive
List<Test2> t=example.getTest().stream()
.flatMap(x -> x.getTest2() == null ? Stream.empty() : x.getTest2().stream())
.collect(Collectors.toList());
System.out.println(t)// It prints only t object
I was expecting two element on with t object and the other one as empty list[]
}
So that later I can do an empty check with the new list
if(isEmpty(example.getTest().stream()
.flatMap(x -> x.getTest2() == null ? Stream.empty() : x.getTest2().stream())
.collect(Collectors.toList())))
result.stream().flatMap(x-> Stream.of(x.getSomeCall())).collect(Collectors.toList()).size()somecallbut you're not using this in your code... and when I said "edit your code to ensure it compiles" I was referring to1stlistas that's not a valid identifier in Java. Further, I can't seem to digest what you're trying to achieve here so if you could provide some more information in regard to the task at hand that would help me and other people in the same position to understand where you're coming from.flatMapand somehow expecting that for each list or null returned by a getSomeCall() in an input stream value you expect to have one element in the output that represents that list or null. flatMap would simply "melt" all those lists into a simple element stream so that input empty lists won't have any presence in the output... typically if null inputs are accepted they would have the same fate.