/** * Collect all elements emitted by this {@link Flux} into a hashed {@link Map} that is * emitted by the resulting {@link Mono} when this sequence completes. * The key is extracted from each element by applying the {@code keyExtractor} * {@link Function}, and the value is extracted by the {@code valueExtractor} Function. * In case several elements map to the same key, the associated value will be derived * from the most recently emitted element. * * <p> * <img class="marble" src="doc-files/marbles/collectMapWithKeyAndValueExtractors.svg" alt=""> * * @param keyExtractor a {@link Function} to map elements to a key for the {@link Map} * @param valueExtractor a {@link Function} to map elements to a value for the {@link Map} * * @param <K> the type of the key extracted from each source element * @param <V> the type of the value extracted from each source element * * @return a {@link Mono} of a {@link Map} of key-element pairs (only including latest * element's value in case of key conflicts) */ public final <K, V> Mono<Map<K, V>> collectMap(Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor) { return collectMap(keyExtractor, valueExtractor, () -> new HashMap<>()); }
/** * Collect all elements emitted by this {@link Flux} into a hashed {@link Map} that is * emitted by the resulting {@link Mono} when this sequence completes. * The key is extracted from each element by applying the {@code keyExtractor} * {@link Function}. In case several elements map to the same key, the associated value * will be the most recently emitted element. * * <p> * <img class="marble" src="doc-files/marbles/collectMapWithKeyExtractor.svg" alt=""> * * @param keyExtractor a {@link Function} to map elements to a key for the {@link Map} * @param <K> the type of the key extracted from each source element * * @return a {@link Mono} of a {@link Map} of key-element pairs (only including latest * element in case of key conflicts) */ public final <K> Mono<Map<K, T>> collectMap(Function<? super T, ? extends K> keyExtractor) { return collectMap(keyExtractor, identityFunction()); }
@Override public Mono<Boolean> multiSet(Map<? extends K, ? extends V> map) { Assert.notNull(map, "Map must not be null!"); return createMono(connection -> { Mono<Map<ByteBuffer, ByteBuffer>> serializedMap = Flux.fromIterable(() -> map.entrySet().iterator()) .collectMap(entry -> rawKey(entry.getKey()), entry -> rawValue(entry.getValue())); return serializedMap.flatMap(connection::mSet); }); }
@Override public Mono<Boolean> multiSetIfAbsent(Map<? extends K, ? extends V> map) { Assert.notNull(map, "Map must not be null!"); return createMono(connection -> { Mono<Map<ByteBuffer, ByteBuffer>> serializedMap = Flux.fromIterable(() -> map.entrySet().iterator()) .collectMap(entry -> rawKey(entry.getKey()), entry -> rawValue(entry.getValue())); return serializedMap.flatMap(connection::mSetNX); }); }
@Override public Mono<Boolean> putAll(H key, Map<? extends HK, ? extends HV> map) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(map, "Map must not be null!"); return createMono(connection -> Flux.fromIterable(() -> map.entrySet().iterator()) // .collectMap(entry -> rawHashKey(entry.getKey()), entry -> rawHashValue(entry.getValue())) // .flatMap(serialized -> connection.hMSet(rawKey(key), serialized))); }
@GetMapping("/routes") public Mono<List<Map<String, Object>>> routes() { Mono<Map<String, RouteDefinition>> routeDefs = this.routeDefinitionLocator.getRouteDefinitions() .collectMap(RouteDefinition::getId); Mono<List<Route>> routes = this.routeLocator.getRoutes().collectList(); return Mono.zip(routeDefs, routes).map(tuple -> {
@Override public Mono<RedisSession> findById(String id) { String sessionKey = getSessionKey(id); // @formatter:off return this.sessionRedisOperations.opsForHash().entries(sessionKey) .collectMap((e) -> e.getKey().toString(), Map.Entry::getValue) .filter((map) -> !map.isEmpty()) .map(new SessionMapper(id)) .filter((session) -> !session.isExpired()) .map(RedisSession::new) .switchIfEmpty(Mono.defer(() -> deleteById(id).then(Mono.empty()))); // @formatter:on }
@Test public void collectMapEmpty() { StepVerifier.create(Flux.<Pojo>empty().collectMap(p -> p.id)) .assertNext(d -> assertThat(d).isEmpty()) .verifyComplete(); }
@Test public void collectMapCallable() { StepVerifier.create(Mono.fromCallable(() -> new Pojo("test", 1L)) .flux() .collectMap(p -> p.id)) .assertNext(p -> assertThat(p).containsOnlyKeys(1L) .containsValues(new Pojo("test", 1L))) .verifyComplete(); }
@Test public void collectMap() { StepVerifier.create(Flux.just(new Pojo("test", 1L), new Pojo("test", 2L), new Pojo("test2", 3L)) .collectMap(p -> p.id)) .assertNext(d -> assertThat(d).containsKeys(1L, 2L, 3L) .containsValues(new Pojo("test", 1L), new Pojo("test", 2L), new Pojo("test2", 3L))) .verifyComplete(); }
@Override public Mono<Health> health() { return Flux.fromIterable(this.registry.getAll().entrySet()) .flatMap((entry) -> Mono.zip(Mono.just(entry.getKey()), entry.getValue().health().compose(this.timeoutCompose))) .collectMap(Tuple2::getT1, Tuple2::getT2) .map(this.healthAggregator::aggregate); }
/** * @param keyExtractor * @param valueExtractor * @return * @see reactor.core.publisher.Flux#collectMap(java.util.function.Function, java.util.function.Function) */ public final <K, V> Mono<Map<K, V>> collectMap(Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor) { return boxed.collectMap(keyExtractor, valueExtractor); } /**
/** * @param keyExtractor * @param valueExtractor * @param mapSupplier * @return * @see reactor.core.publisher.Flux#collectMap(java.util.function.Function, java.util.function.Function, java.util.function.Supplier) */ public final <K, V> Mono<Map<K, V>> collectMap(Function<? super T, ? extends K> keyExtractor, Function<? super T, ? extends V> valueExtractor, Supplier<Map<K, V>> mapSupplier) { return boxed.collectMap(keyExtractor, valueExtractor, mapSupplier); } /**
@Override public Mono<Boolean> multiSetIfAbsent(Map<? extends K, ? extends V> map) { Assert.notNull(map, "Map must not be null!"); return createMono(connection -> { Mono<Map<ByteBuffer, ByteBuffer>> serializedMap = Flux.fromIterable(() -> map.entrySet().iterator()) .collectMap(entry -> rawKey(entry.getKey()), entry -> rawValue(entry.getValue())); return serializedMap.flatMap(connection::mSetNX); }); }
@Override public Mono<Boolean> multiSet(Map<? extends K, ? extends V> map) { Assert.notNull(map, "Map must not be null!"); return createMono(connection -> { Mono<Map<ByteBuffer, ByteBuffer>> serializedMap = Flux.fromIterable(() -> map.entrySet().iterator()) .collectMap(entry -> rawKey(entry.getKey()), entry -> rawValue(entry.getValue())); return serializedMap.flatMap(connection::mSet); }); }
@Override public Mono<Boolean> multiSetIfAbsent(Map<? extends K, ? extends V> map) { Assert.notNull(map, "Map must not be null!"); return createMono(connection -> { Mono<Map<ByteBuffer, ByteBuffer>> serializedMap = Flux.fromIterable(() -> map.entrySet().iterator()) .collectMap(entry -> rawKey(entry.getKey()), entry -> rawValue(entry.getValue())); return serializedMap.flatMap(connection::mSetNX); }); }
private static Mono<Map<String, String>> getApplicationsById(CloudFoundryClient cloudFoundryClient, String spaceId) { return requestListApplications(cloudFoundryClient, spaceId) .map(resource -> Tuples.of(resource.getId(), resource.getName())) .collectMap(function((id, name) -> id), function((id, name) -> name)); }
private static Mono<Map<String, String>> getApplicationsByName(CloudFoundryClient cloudFoundryClient, String spaceId) { return requestListApplications(cloudFoundryClient, spaceId) .map(resource -> Tuples.of(resource.getId(), resource.getName())) .collectMap(function((id, name) -> name), function((id, name) -> id)); }
private static Mono<Map<String, String>> getAllSpaces(CloudFoundryClient cloudFoundryClient, String organizationId) { return requestAllSpaces(cloudFoundryClient, organizationId) .map(resource -> Tuples.of(ResourceUtils.getId(resource), ResourceUtils.getEntity(resource).getName())) .collectMap(function((id, name) -> id), function((id, name) -> name)); }
private static Mono<Map<String, String>> getAllDomains(CloudFoundryClient cloudFoundryClient, String organizationId) { return requestAllPrivateDomains(cloudFoundryClient, organizationId) .map(resource -> Tuples.of(ResourceUtils.getId(resource), ResourceUtils.getEntity(resource).getName())) .mergeWith(requestAllSharedDomains(cloudFoundryClient) .map(resource -> Tuples.of(ResourceUtils.getId(resource), ResourceUtils.getEntity(resource).getName()))) .collectMap(function((id, name) -> id), function((id, name) -> name)); }