英文:
Iterating using ForEach in Spring -boot
问题
我正在尝试迭代遍历数据库表的存储库,一旦在那里,访问其中一行以最终检索所需信息。
因此,我首先在整个数据库中浏览了我的存储库,应用了 findAll()
方法;然后在那里,我使用了 stream().forEach()
,最终使我定位在每个项目内,能够检索任何类型的信息,例如访问其列表和其他内容。
但是,这会抛出一个异常
所需类型:Object
提供类型:void
以下是该函数:
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);
}
这是否意味着我应该返回某个东西而不是 void?
我感谢任何帮助,并提前致谢。
英文:
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);
}
Does that mean I should return something instead of void?
I appreciate any help, and thanks in advance
答案1
得分: 2
dto.put("player_shots_detail", playerCrabRepository.findAll().stream().map(playerCrab -> playerCrab.getDiceCrabList()).flatMap(diceCrab -> makeDiceCrabMiniDto(diceCrab)).collect(Collectors.toList()));
英文:
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()));
答案2
得分: 0
forEach在Stream上的哨兵操作不返回任何内容,因为它的返回类型是void。请改用collect。您的Map需要一个对象作为值,而您的操作将因为forEach不返回任何内容而无法实现。
Java文档供您参考:https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#forEach-java.util.function.Consumer-
英文:
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-
答案3
得分: 0
在Java中,foreach
循环的返回类型是void
。您可以使用流的map
和flatmap
函数来处理上述用例。
英文:
Return type for foreach in java in void. You can use map and flatmap functions of streams for the above use case.
答案4
得分: 0
错误表示您的函数的类型是void,而您尝试将其作为值放入映射中。
简而言之,foreach块不返回任何内容。在这种情况下,您必须以以下方式存储响应。
当前代码为:
List<T> data = playerCrabRepository.findAll().stream()
.foreach(playerCrab -> {
playerCrab.getDiceCrabList.stream()
.map(makeDirceCrabMiniDto(diceCrab))
.collect(Collectors.toList())
})
dto.put("player_shots_detail", data);
请改用map而不是foreach,并返回它们,然后进行收集。
在这种情况下,您必须将结果存储在列表中,
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);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论