Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto refactor for loop with Service call into Stream?

how would you refactor this code into Streams?

for (ConditionEntity conditionEntity : dbData.getContent()) {
    AdminCondition adminCondition = 
            dataEntityMapper.conditionEntitytoModel(conditionEntity);

    this.myService.addBasicInterest(adminCondition); // <=== this is my Problem
    conditionList.add(adminCondition);
}

I've this so far:

dbData.getContent().stream()
    .map(conditionEntity -> dataEntityMapper.conditionEntitytoModel(conditionEntity))
    .collect(conditionList);

Thx for help!

like image 240
Vladimir Avatar asked Mar 05 '23 06:03

Vladimir


1 Answers

I would prefer the following solution:

List<ConditionEntity> conditions = dbData.getContent().stream()
        .map(dataEntityMapper::conditionEntitytoModel)
        .collect(Collectors.toList());

conditions.forEach(this::addBasicInterest);
like image 197
Glains Avatar answered Mar 16 '23 01:03

Glains