Java 如何在Mono上调用entrySet方法<;地图<;实体,整数>&燃气轮机;
假设我有一个方法findAll,它从Java 如何在Mono上调用entrySet方法<;地图<;实体,整数>&燃气轮机;,java,functional-programming,reactive-programming,spring-webflux,Java,Functional Programming,Reactive Programming,Spring Webflux,假设我有一个方法findAll,它从ReactiveMongoRepository返回Flux 下面我使用传统的java流方法来查找@Naman推荐的x首热门歌曲: return songRepository.findAll() .collect(Collectors.toMap(Function.identity(), this::getSumOfVotes)) .entrySet().stream() --entrySet method is not recognized b
ReactiveMongoRepository
返回Flux
下面我使用传统的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));
但我尽量避免重新包装我的:
songRepository.findAll()
.collect(Collectors.toMap(Function.identity(), this::getSumOfVotes))
到Flux.fromIterable
如果您能给我一些关于我做错了什么的建议或如何跳过这个障碍的提示,我将不胜感激。这是Map
,它有entrySet
方法;如果要对Mono
的内容应用某些转换,请使用:
Mono歌曲=。。。;
谢谢你的回答。这就解决了问题。利用一个问题,我会问另一个问题。如果我想使用.sorted(Map.Entry.comparingByValue().reversed())
作为我流程中的下一步,我应该使用doOnNext
操作符还是其他什么?实际上,它应该是传递给Map
的操作的一部分(或单独的Map
)。其余的(.sorted().limit().collect()
)也应该如此。谢谢您的回复。问题是,在使用.map(map::entrySet)
将其映射到entrySet之后,并没有类似sort的可用方法。在entrySet
方法使用之后的正常流中,我们应该通过调用.stream
来重新创建流,但这里没有明显的解决方案。你知道怎么在Mono上做吗?没关系。我不能对单个单体对象进行排序。我使用了.flatMapMany(Flux::fromIterable)
,现在可以使用sort()
方法。如果您需要做的不仅仅是调用单个方法,那么可以使用lambda而不是方法引用,如Map::entrySet
:Map->Map.entrySet().stream()…
songRepository.findAll()
.collect(Collectors.toMap(Function.identity(), this::getSumOfVotes))
Mono<Map<Song, Integer>> songs = ...;
Mono<Set<Map.Entry<Song, Integer>> = songs.map(Map::entrySet);