使用Spring Boot中的ForEach进行迭代。

huangapple go评论77阅读模式
英文:

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&lt;Map&lt;String, Object&gt;&gt; allshots(Authentication authentication) {
        Map&lt;String, Object&gt; dto = new HashMap&lt;&gt;();



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


        return new ResponseEntity&lt;&gt;(dto, HttpStatus.CREATED);
    }

使用Spring Boot中的ForEach进行迭代。

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(&quot;player_shots_detail&quot;, playerCrabRepository.findAll().stream().map(playerCrab -&gt; playerCrab.getDiceCrabList()).flatMap(diceCrab -&gt; 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。您可以使用流的mapflatmap函数来处理上述用例。

英文:

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&lt;T&gt; data = playerCrabRepository.findAll().stream()
	.foreach(playerCrab -&gt; {
		playerCrab.getDiceCrabList.stream()
			.map(makeDirceCrabMiniDto(diceCrab))
			.collect(Collectors.toList())
	})
dto.put(&quot;player_shots_detail&quot;, data);

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

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

List&lt;T&gt; data = playerCrabRepository.findAll().stream()
	.map(playerCrab -&gt; {
		return playerCrab.getDiceCrabList.stream()
			.map(makeDirceCrabMiniDto(diceCrab))
			.collect(Collectors.toList())
	}).collect(Collectors.toList());
dto.put(&quot;player_shots_detail&quot;, data);

huangapple
  • 本文由 发表于 2020年7月23日 18:03:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63051680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定