Peter Penzov
Peter Penzov

Reputation: 1670

Replace value using streams

I have Spring Data Entity where I get paged result:

classItemsService.findAll(spec, pageable).map(classItemsMapper::toFullDTO);

Using MapStrict framework I filter the values returned from the API:

@Mapper(config = BaseMapperConfig.class)
public interface ClassItemsMapper {

    ClassItemsFullDTO toFullDTO(ClassItems classItems);
}

DTO:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
public class ClassItemsFullDTO {
    ........
    private String productVideo;
    ........
}

The question is how I can replace second time the values returned from the DB and replacing then with some hardcoded value. For example I suppose that it should be like this:

Page<ClassItemsFullDTO> page = classItemsService.findAll(spec, pageable).map(classItemsMapper::toFullDTO).stream()
            .forEach(classItemsFullDTO -> classItemsFullDTO.setProductVideo("unsubscribed"));

Can you help me to fix the issue?

Now I get this issue:

Required type: Page <ClassItemsFullDTO>
Provided: void

Upvotes: 2

Views: 506

Answers (1)

fps
fps

Reputation: 34460

Stream.forEach method does not return anything, i.e. it returns void. So the assignment you are doing doesn't work with it. Remove Page<ClassItemsFullDTO> page = and it will work.

If you need to keep a reference to the result, I suggest you don't use a Stream:

Page<ClassItemsFullDTO> page = classItemsService.findAll(spec, pageable)
        .map(classItemsMapper::toFullDTO);

page.forEach(dto -> dto.setProductVideo("unsubscribed"));

Upvotes: 3

Related Questions