Java 8 - Streams Nested ForEach with different Collection

24,221

First, try to adhere to the Java naming conventions, as your upper case variable names make it really hard to read your code. Second, it’s a good thing that you want to learn about Stream API but you should not ignore the basics of the pre-Java 8 Collection APIs.

It’s not useful to iterate over an entrySet() when you are only interested in either, keys or values. You do it two times within a small piece of code.

At the first appearance you can replace

for (Map.Entry<String, SalesQuot> Quot: Quotations.entrySet()){
    SalesQuot sapQuot = Quot.getValue();

with the simpler

for (SalesQuot sapQuot: Quotations.values()){

At the second, the entire

for(Map.Entry<String,SalesQuotPosition> quotp: sapQuot.getPosition().entrySet()){
    tempQuotPos.add(quotp.getValue());
}

can be replaced by

tempQuotPos.addAll(sapQuot.getPosition().values());

Thus even without streams, your code can be simplified to

for (ClassInq simInq : this.Inq){
        if (!simInq.isClosed() && !simInq.isDenied()){      
            for (SalesQuot sapQuot: Quotations.values()){
                if (sapQuot.getInquiryDocumentNumber().compareTo(simInq.getSapInquiryNumber()) == 0){
                    simInq.setSAPQuotationNumber(sapQuot.getQuotationDocumentNumber());
                    tempInqAndQuot.add(simInq);
                    tempQuotPos.addAll(sapQuot.getPosition().values());
                }
            }
        }
    }

though it’s still not clear what it is supposed to do and whether it’s correct. Besides the errors and suspicions named in the comments to your question, modifying the incoming values (esp. from the outer loop) does not look right.

It’s also not clear why you are using ….compareTo(…)==0 rather than equals.

However, it can be straight-forwardly rewritten to use streams without changing any of the code’s logic:

this.Inq.stream().filter(simInq -> !simInq.isClosed() && !simInq.isDenied())
  .forEach(simInq -> Quotations.values().stream().filter(sapQuot ->
   sapQuot.getInquiryDocumentNumber().compareTo(simInq.getSapInquiryNumber())==0)
   .forEach(sapQuot -> {
      simInq.setSAPQuotationNumber(sapQuot.getQuotationDocumentNumber());
      tempInqAndQuot.add(simInq);
      tempQuotPos.addAll(sapQuot.getPosition().values());
    })
  );

Still, I recommend cleaning up the original logic first before rewriting it for using other APIs. The stream form would greatly benefit from a more precise definition of what to achieve.

Share:
24,221
user1796346
Author by

user1796346

Updated on November 10, 2020

Comments

  • user1796346
    user1796346 over 3 years

    I try to understand the new Java 8 Streams and I tried for days to transfer nested foreach loops over collection in Java 8 Streams.

    Is it possible to refactor the following nested foreach loops including the if-conditions in Java-8-Streams?

    If yes what would it look like.

    ArrayList<ClassInq> Inq = new ArrayList<>();
    TreeMap<String, SalesQuot> Quotations = new TreeMap<>();
    
    ArrayList<ClassInq> tempInqAndQuot = new ArrayList<>();
    ArrayList<SalesQuot> tempQuotPos = new ArrayList<>();   
    
    for(ClassInq simInq : this.Inq) {
        if(!simInq.isClosed() && !simInq.isDenied()) {
            for(Map.Entry<String, SalesQuot> Quot: Quotations.entrySet()) {
    
                SalesQuot sapQuot = Quot.getValue();
    
                if(sapQuot.getInquiryDocumentNumber().compareTo(simInq.getSapInquiryNumber()) == 0) {
    
                    simInq.setSAPQuotationNumber(sapQuot.getQuotationDocumentNumber());
                    tempInqAndQuot.add(simInq);
    
                    for(Map.Entry<String, SalesQuotPosition> quotp : sapQuot.getPosition().entrySet()) {
                        tempQuotPos.add(quotp.getValue());
                    }
                }
            }
        }
    }
    

    Thanks a lot for your help.

    BR

  • user1796346
    user1796346 over 9 years
    Thanks for your help. I'm a java beginner and I try to first get the program working and in the next step to improve/refactor it. Thanks for all your hints. So first I will clean up the java naming convention issues and next I will try to clean my code.