1

I am writing a code to loop through multiple different type of lists and create another based on some business validations using java 8 lambda expressions.

Business Validation/Logic:

there are 2 lists List<ServiceMap> listA and List<Integer> listB
1. if the element in listA not present in listB then deactivate that record in DB.
2. if the element in listA present in listB then activate that record in DB.
3. if the element in listB not present in listA then create new record in DB.

Model class:

class ServiceMap{
    Integer serviceMapId;
    Integer serviceId;
    boolean isActive;
    Integer updatedBy;
    Calendar updatedDate;
}

Code:

List<ServiceMap> listA = getServiceMaps();//Will get from database
List<Integer> listB = Arrays.asList(1, 10, 9);//Will get from client
List<ServiceMap> listC = new ArrayList<>();//Building new list by validating records from both lists above
listA.stream().forEach(e -> {
    //noneMatch means we have to deactivate record in DB
    if (listB.parallelStream().noneMatch(x -> x == e.getServiceId())) {
        ServiceMap recordToDeactivate = e;
        e.setIsActive(false);
        listC.add(recordToDeactivate);
        return;
    }

    listB.stream().forEach(x -> {
        // if the record already added to listC then continue to next record
        if (listC.stream().anyMatch(e2->e2.getServiceId() == x)) {
            return;
        }
        //Matched means we have to activate record in DB
        if (x == e.getServiceId()) {
            ServiceMap recordToActivate = e;
            e.setIsActive(true);
            listC.add(recordToActivate);
            return;
        } else {
            //Not Matched means create new record in DB
            ServiceMap newM = new ServiceMap();
            newM.setIsActive(true);
            newM.setServiceId(x);
            listC.add(newM);
        }
    });

});

Is there any way to achieve above logic in simplest way ?

1 Answer 1

2

Not sure why you stream through list B for every item in list A. Nor why you even use Streams.

List<Integer> notInA = new ArrayList<>(listB);
listA.forEach(sm -> {
         notInA.remove(a.getServiceId());
         sm.setActive(listB.contains(sm.getServiceId()));
         listC.add(sm);
      });

notInA.forEach(id -> {
         ServiceMap newMap = new ServiceMap(id);
         newMap.setActive(true);
         listC.add(newMap);
      });
Sign up to request clarification or add additional context in comments.

3 Comments

thank you very much, really helped me a lot. One question: you mentioned "listA.stream().peek(a -> notInA.remove(a.getServiceId()))". can you please explain
@Krish I removed that since I edited to not use a stream at all. But as an explanation, peek is to be able to access every element of the stream before you actually consume it - ie somewhat like a forEach without the stream being terminated. It's not really good style to use it for things other than debugging which is one of the reason I changed the answer to use forEach on the list directly.
Thanks for the explanation. I am new to java8 lambda expressions and stream.

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.