问题描述
所以我有一个 contactId 对象,我将它转换为通量,然后我分别使用每个 Id 查询联系人详细信息,然后使用 collectList 方法将其转换为 Mono 并将其映射到另一个对象,我们称之为 DomainContactDetails 像这样。
Flux.just(identifier.regContactId,identifier.billingContactId,identifier.adminContactId,identifier.techContactId)
.flatMap { resellerGetContact(it) }
.collectList()
.map { DomainContactDetails.fromList(it) }
.toFuture()
无论如何都可以将通量元素收集到键映射而不是列表中。现在我使用列表索引来确定哪些数据属于该属性
data class DomainContactDetails(
val registrantContact: ContactInfo,val billingContact: ContactInfo,val adminContact: ContactInfo,val techContact: ContactInfo
) {
companion object {
fun fromList(contacts: List<ContactInfo>) = DomainContactDetails(
registrantContact = contacts[0],billingContact = contacts[1],adminContact = contacts[2],techContact = contacts[3]
)
}
}
我希望它是关键映射,这样可能会更好一些
fun fromKeyMap(contacts: Map<String,ContactInfo>) = DomainContactDetails(
registrantContact = contacts["reg"],billingContact = contacts["bil"],adminContact = contacts["adm"],techContact = contacts["tec"]
)
解决方法
我完全不确定您的解决方案,但既然您问收集到 Map
,这里是一个答案。请参阅 Flux.collect()
运算符:
/**
* Collect all elements emitted by this {@link Flux} into a container,* by applying a Java 8 Stream API {@link Collector}
* The collected result will be emitted when this sequence completes.
*
* <p>
* <img class="marble" src="doc-files/marbles/collectWithCollector.svg" alt="">
*
* @param collector the {@link Collector}
* @param <A> The mutable accumulation type
* @param <R> the container type
*
* <p><strong>Discard Support:</strong> This operator discards the intermediate container (see {@link Collector#supplier()} upon
* cancellation,error or exception while applying the {@link Collector#finisher()}. Either the container type
* is a {@link Collection} (in which case individual elements are discarded) or not (in which case the entire
* container is discarded). In case the accumulator {@link BiConsumer} of the collector fails to accumulate
* an element into the intermediate container,the container is discarded as above and the triggering element
* is also discarded.
*
* @return a {@link Mono} of the collected container on complete
*
*/
public final <R,A> Mono<R> collect(Collector<? super T,A,? extends R> collector) {
因此,您可能只需要利用现有的 Collectors.toMap()
API。