作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个方法findAll,它返回 Flux<Song>
来自ReactiveMongoRepository<Song, String>
.
下面我使用传统的 java 流方法来查找 @Naman 建议的 x 得票最多的歌曲:
return songRepository.findAll()
.collect(Collectors.toMap(Function.identity(), this::getSumOfVotes))
.entrySet().stream() --entrySet method is not recognized by IntelliJ
.sorted(Map.Entry.<Song,Integer>comparingByValue().reversed())
.limit(numberOfTopSongs)
.collect(Collectors.toMap(e -> e.getKey().getId(), Map.Entry::getValue, (a, b) -> a, LinkedHashMap::new));
以上解决方案适用于 Iterable
, Set
和List
但是当我假装调用方法 entrySet
时就会出现问题上Mono<Map<Song, Integer>>
。这是我第一次深入了解 Web Flux
这就是为什么我有点困惑为什么方法 entrySet
不适用于 Mono
类型。
我发现了一段代码,例如:
Mono<Map<ByteBuffer, ByteBuffer>> serializedMap = Flux.fromIterable(() -> map.entrySet().iterator())
.collectMap(entry -> rawKey(entry.getKey()), entry -> rawValue(entry.getValue()));
但我尽量避免重新包装我的:
songRepository.findAll()
.collect(Collectors.toMap(Function.identity(), this::getSumOfVotes))
至 Flux.fromIterable
再次。
如果您对我做错的事情提出建议或如何跳过此障碍,我将不胜感激。
最佳答案
这是 Map<Song, Integer>
其中有entrySet
方法;如果您想对 Mono
的内容应用一些转换,使用 map
:
Mono<Map<Song, Integer>> songs = ...;
Mono<Set<Map.Entry<Song, Integer>> = songs.map(Map::entrySet);
请注意map
是这种方法的常见名称,可以在 Flux
上找到。 , Stream
等
关于java - 如何在Mono<Map<Entity, Integer>>上调用entrySet方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60412780/
我是一名优秀的程序员,十分优秀!