Home > Mobile >  java stream map class with relationship
java stream map class with relationship

Time:02-02

I'm trying to to sum values of my class with join with another class with stream and map.

campaigns.stream().mapToLong(SmoochActiveCampaign::getSmoochActiveCampaignReport::getQtyPhoneConfirmed).sum();

my old code is this way...

    Long qtyMsgsApi = Long.valueOf("0");
    
    if (campaigns.size() > 0) {
        for (int indexTag = 0; indexTag < campaigns.size(); indexTag  ) {
            

                qtyMsgsApi = qtyMsgsApi   campaigns.get(indexTag).getSmoochActiveCampaignReport().getQtyPhoneConfirmed();
            }
        }   
    }

CodePudding user response:

You can do it like so.

  • stream the list
  • map to a long
  • sum them.
long qtyMsgsApi = campaigns.stream()
        .mapToLong(campaign -> campaign.getSmoochActiveCampaignReport()
                .getQtyPhoneConfirmed())
        .sum();

CodePudding user response:

You can use this code.

 campaigns.stream().mapToLong(campaign-> {
                return campaign.getSmoochActiveCampaignReport().getQtyPhoneConfirmed();
            }).sum();
  •  Tags:  
  • Related