Enrique GF
Enrique GF

Reputation: 1295

Iterating using ForEach in Spring -boot

I'm trying to iterate over a repository of database table and once there, access one of the row to finally retrieve the information i need. Thus I first went to my repository over all those elements in the database, applied findAll() method; then being there I used stream().foreach(), which eventually positioned me inside each item being able to retrieve any kind of information, like accessing its lists and other things. But that throws an exception

Required type:Object
Provided:void

here is the function :

public ResponseEntity<Map<String, Object>> allshots(Authentication authentication) {
        Map<String, Object> dto = new HashMap<>();



        dto.put("player_shots_detail", playerCrabRepository.findAll().stream().forEach(playerCrab -> {  playerCrab.getDiceCrabList().stream()
        .map(diceCrab -> makeDiceCrabMiniDto(diceCrab)).collect(Collectors.toList());}));


        return new ResponseEntity<>(dto, HttpStatus.CREATED);
    }

enter image description here

Does that mean I should return something instead of void? I appreciate any help, and thanks in advance

Upvotes: 0

Views: 12819

Answers (4)

Dhruv
Dhruv

Reputation: 159

The return type for forEach in java is void. You can use map and flatmap functions of streams for the above use case.

Upvotes: 0

Dhwanil Patel
Dhwanil Patel

Reputation: 2573

Error denotes that your function is type void which response you try to place as value in map.

In short foreach block return nothing. In such case you have to store response in following way.

Current code is :

List<T> data = playerCrabRepository.findAll().stream()
    .foreach(playerCrab -> {
        playerCrab.getDiceCrabList.stream()
            .map(makeDirceCrabMiniDto(diceCrab))
            .collect(Collectors.toList())
    })
dto.put("player_shots_detail", data);

Use map instead of foreach and return them & collect it.

In such condition you have to store your result in list,

List<T> data = playerCrabRepository.findAll().stream()
    .map(playerCrab -> {
        return playerCrab.getDiceCrabList.stream()
            .map(makeDirceCrabMiniDto(diceCrab))
            .collect(Collectors.toList())
    }).collect(Collectors.toList());
dto.put("player_shots_detail", data);

Upvotes: 0

Niru
Niru

Reputation: 742

forEach a sentinel operation on Stream doesn't return anything as it's return type is void. Use collect instead. Your Map needs an object as value while your operation will return nothing due to forEach.

Java Docs for your reference. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#forEach-java.util.function.Consumer-

Upvotes: 0

Hiep Le
Hiep Le

Reputation: 373

forEach return void but dto.put requird an object. try replace forEach with map

dto.put("player_shots_detail", playerCrabRepository.findAll().stream().map(playerCrab -> playerCrab.getDiceCrabList()).flatMap(diceCrab -> makeDiceCrabMiniDto(diceCrab)).collect(Collectors.toList()));

Upvotes: 2

Related Questions