2

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())))
13
  • why not this result.stream().flatMap(x-> Stream.of(x.getSomeCall())).collect(Collectors.toList()).size() Commented Jan 9, 2019 at 18:22
  • I got the size as 2..How do I iterate this list..I am getting some compile time error while iterating.I am new to Java 8. I want to check the list value.. Commented Jan 9, 2019 at 18:47
  • 1
    @Subham you defined a list called somecall but you're not using this in your code... and when I said "edit your code to ensure it compiles" I was referring to 1stlist as 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. Commented Jan 9, 2019 at 18:54
  • As said above it is quite unclear what you expect to achieve here without giving us some examples of inputs and expected outputs. What is most confusing to me is the conflict between using flatMap and 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. Commented Jan 9, 2019 at 19:59
  • 1
    ... in other words if the example input getSomeCall() invocations would return a singleton list (with only one element) for the first input and a null for the second input the most straightforward reasonable output would have only one element. .... if the first input has 5 elements, a second input is null, a third input is empty and the fourth input has 2 elements the most reasonable output would have 7 elements... the ones present in the first and last inputs. Commented Jan 9, 2019 at 20:02

2 Answers 2

2

It's often simpler and more readable to break up a complex stream step into into multiple simple ones:

list1.stream()
     .map(SomeCall::getSomeCall)
     .filter(Objects::nonNull)
     .flatMap(Collection::stream)   // or List::stream if it's a list
     .collect(...)
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting the list size as 1.I was expecting list size value as 2..The 1st value with some object and the second one as an empty list
This should be the accepted answer based on the original question. @Subham please provide a more definitive example of the data you're using that is not working.
1

You could instead find such a sum simply using:

int size = stList.stream() // variable renamed not to start with numeric
        .mapToInt(st -> Optional.ofNullable(st.getSomeCall()).map(List::size).orElse(0))
        .sum();

Updated with question:

System.out.println(example.getTest().stream() // variable renamed not to start with numeric
        .mapToInt(st -> Optional.ofNullable(st.getTest2()).map(List::size).orElse(0))
        .sum());

get the list of value instead of size

If you were to get the List<Test2> as a result, your existing code is good enough, though you can also get it as :

List<Test2> list = example.getTest().stream()
        .map(a -> a.getTest2() == null ? new ArrayList<Test2>() : a.getTest2())
        .flatMap(List::stream)
        .collect(Collectors.toList());

12 Comments

@HadiJ because st.getSomeCall() could be null. if null consider the value as 0 or else the actual size of the List.
How to get the list of value instead of size??
@Subham Though I've updated the answer seems like the code in the question has been changed a bit, it wouldn't compile now. Make sure to fix it for complete understanding.
@It gives one element as a result..Dont know why the null value which is supposed to changed in to empty list is not added in to the new list..
But that overhead is pointless, as it doesn’t change the result, hence, doesn’t solve the OP’s problem. The OP’s problem seems to be that they are comparing to output of a loop printing unflattened original lists with a Stream operation collecting to a flattened List<Test2>. Of course, doing entirely different things may produce entirely different outcome. But it doesn’t help to provide an answer suggesting to every reader to produce an unnecessary overhead…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.