1

I want to convert this piece code into streams

List<ECSUSU> listOfUSUs = msccRequest.getUsedServiceUnit();
if (listOfUSUs != null) {
     ECSUSU usedServiceUnit = listOfUSUs.get(0);
     if (usedServiceUnit.getCcTime() != null && usedServiceUnit.getCcTime() > 0) {
          return UnitType.SECONDS;
      } else {
          return UnitType.BYTES;
      }
}

I would thought this will work but I am getting NullPointerException. msccRequest.getUsedServiceUnit() might return null , that's why I need the check

 msccRequest.getUsedServiceUnit().stream()
                    .filter(list -> list != null)
                    .limit(1)
                    .map(usedServiceUnit -> {
                        if (usedServiceUnit.getCcTime() != null && usedServiceUnit.getCcTime() > 0) {
                            return UnitType.SECONDS;
                        } else {
                            return UnitType.BYTES;
                        }
                    });
0

2 Answers 2

2

filter applies to individual elements of the Stream (A Stream iterates over each element in the list), so if msccRequest.getUsedServiceUnit() returns null, the .stream() invocation will throw an NPE. If you need to ensure that the list itself is not null, I suggest that you wrap it in an Optional, like this:

Optional<List<ECSUSU>> list = Optional.ofNullable(msccRequest.getUsedServiceUnit())

Then use ifPresent to implement the iteration:

list.ifPresent(notNullListForSure -> notNullListForSure.stream())

etc

Sign up to request clarification or add additional context in comments.

3 Comments

yeah but why not a standard if statement and perform the stream operation only if the list is not null?? List<ECSUSU> listOfUSUs = msccRequest.getUsedServiceUnit(); if(listOfUSUs != null) //do stream stuff
That's an option (pun intended) if you prefer the non-functional approach. Since the OP wanted to convert to Stream, I simply extended it in the same fashion.
Thanks, i've not got a chance to try this out , i will reply asap.
1

If msccRequest.getUsedServiceUnit() returns null, the attempt to call the stream() method on that will throw a NullPointerException. I expect the NPE comes from that. You need to check for null before calling the stream() method.

Also note that your two pieces of code are not equivalent (even if the above problem is fixed). The first code only checks the first element of the list, while the second takes the first non-null element.

Also, you are only interested in a single element of the list. Then findFirst seems to be a better choice than limit.

List<ECSUSU> listOfUSUs = msccRequest.getUsedServiceUnit();
if (listOfUSUs != null) {
    return listOfUSUs.stream()
        .filter(Objects::nonNull)
        .findFirst()
        .map(usedServiceUnit -> {
                if (usedServiceUnit.getCcTime() != null && usedServiceUnit.getCcTime() > 0) {
                    return UnitType.SECONDS;
                } else {
                    return UnitType.BYTES;
                }
            })
        .orElse(/* default value */);
}

1 Comment

Thanks, i've not got a chance to try this out , i will reply asap.

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.