如何在 SmallRye Mutiny 中对多个元素进行排序。

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

How do i sort a Multi in smallrye mutiny

问题

其他类似于Project Reactor的响应式库提供了用于处理发布者的排序方法,但在Mutiny中却没有类似的方法。
甚至他们的文档中都没有提到这一点。

目前,我通过以下方式实现了所说的功能:

multi.collectItems()
.asList()
.map(
list -> {
  list.sort();
  return list;
})
.convert()
.with(listUni -> Multi.createFrom().iterable(listUni.await().indefinitely()))

是否有更好的方法来实现相同的功能?

英文:

Other reactive libraries like project reactor offer sort methods for Publishers but there is no such method in mutiny.
Their documentation doesn't even talk about it.

> https://smallrye.io/smallrye-mutiny

Right now i'm achieving the said functionality by doing this

multi.collectItems()
.asList()
.map(
list -> {
  list.sort();
  return list;
})
.convert()
.with(listUni -> Multi.createFrom().iterable(listUni.await().indefinitely()))

Is there a better way to do the same ?

答案1

得分: 2

我不相信有一种内置/更好的方法来做这件事。

Mutiny通常以其核心中的“简化”操作符为傲,让您根据需要构建其他更复杂的操作符。他们试图避免出现反应堆的情况,其中核心类型上有100多种方法,如果没有很多背景知识,很难知道哪些方法是相关的。

在我看来,这并不是坏事。其他反应式框架确实内置了这些sort()操作符,但存在一种危险 - 人们会认为它们仍然可以将它们视为无限的、自动排序的发布者,因为在任何地方都没有集合的迹象。当然,内部这些框架必须维护一个基础集合来对数据进行排序,然后在流完成时输出该集合的内容。然而,这并不太明显,对这一事实的无知往往会导致意外的减速和OutOfMemoryError。相反,在您的示例中,立即就能看出这个流使用了一个底层集合来对数据进行排序。

在您的示例中,我只会做一个小改动,但与您的问题不太相关 - 我会使用:

list.stream().sorted().collect(Collectors.toList())

...在您的map调用中,而不是对可变列表进行排序。在反应式流中修改数据结构有点不妥。

英文:

I don't believe there is a built-in/better way to do this.

Mutiny generally prizes itself on having a "cut-down" set of operators in its core, and letting you build up other, more complex operators as needed. They're trying to avoid the reactor situation where you have 100+ methods on a couple of core types, and without a lot of background knowledge it's often difficult to know what ones are relevant.

IMHO that's no bad thing. Other reactive frameworks definitely have these built-in sort() operators, but there's a danger here - people assume they can still treat them as infinite, magically sorted publishers, because there's no sign of any collections anywhere. You can't of course - internally these frameworks have to maintain an underlying collection to sort the data, and then just output the contents of that when the stream is complete. This isn't that clear however, and ignorance of this fact can often lead to unintended slowdowns and OutOfMemoryError. On the contrary, in your example, it's immediately obvious that this stream uses an underlying collection for sorting your data.

There's just one minor thing I'd change in your example, but not really related to your question - I'd use:

list.stream().sorted().collect(Collectors.toList())

...in your map call instead of sorting a mutable list. Mutating data structures in a reactive stream is a bit of a code smell.

答案2

得分: 1

另一种方法我使用过 - 将多重映射组合到 TreeMap 中,只要它们使用 .collect().in(TREE_MAP_SUPPLIER, MULTI_MAP_ACCUMULATOR) 来到时。

英文:

Another approach I used - combine multis into TreeMap as soon as they come with .collect().in(TREE_MAP_SUPPLIER, MULTI_MAP_ACCUMULATOR)

答案3

得分: 0

  1. 将多重列表转换为单一列表。
  2. 根据单一列表进行排序。
  3. 将单一列表转换为多重列表。
multi.collect().asList() //1
.onItem()
.transform(list -> list.stream().sorted(Comparator.comparing(Item::getId)).toList()) //2
.onItem()
.transformToMulti(list -> Multi.createFrom().iterable(list)); //3
英文:
  1. Convert the Multi to Uni (list).
  2. Sort the list from Uni.
  3. Convert the Uni to a Multi
multi.collect().asList() //1
.onItem()
.transform(list -> list.stream().sorted(Comparator.comparing(item::getId)).toList()) //2
.onItem()
.transformToMulti(list -> Multi.createFrom().iterable(list)); //3

huangapple
  • 本文由 发表于 2020年10月16日 02:34:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64377747.html
匿名

发表评论

匿名网友

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

确定