Apply a stream of mappers to another stream in Java8 - java

In Java8 I have a stream and I want to apply a stream of mappers.
For example:
Stream<String> strings = Stream.of("hello", "world");
Stream<Function<String, String>> mappers = Stream.of(t -> t+"?", t -> t+"!", t -> t+"?");
I want to write:
strings.map(mappers); // not working
But my current best way of solving my task is:
for (Function<String, String> mapper : mappers.collect(Collectors.toList()))
strings = strings.map(mapper);
strings.forEach(System.out::println);
How can I solve this problem
without collecting the mappers into a list
without using a for loop
without breaking my fluent code

Since map requires a function that can be applied to each element, but your Stream<Function<…>> can only be evaluated a single time, it is unavoidable to process the stream to something reusable. If it shouldn’t be a Collection, just reduce it to a single Function:
strings.map(mappers.reduce(Function::andThen).orElse(Function.identity()))
Complete example:
Stream<Function<String, String>> mappers = Stream.of(t -> t+"?", t -> t+"!", t -> t+"?");
Stream.of("hello", "world")
.map(mappers.reduce(Function::andThen).orElse(Function.identity()))
.forEach(System.out::println);

Related

Difference between Mono.then and Mono.flatMap/map

Say I want to call a webservice1 and then call webservice2 if the first was successful.
I can do the following (just indicative psuedo code) :-
Mono.just(reqObj)
.flatMap(r -> callServiceA())
.then(() -> callServiceB())
or
Mono.just(reqObj)
.flatMap(r -> callServiceA())
.flatMap(f -> callServiceB())
What is the difference between the two, when using the mono.just() for a single element?
flatMap should be used for non-blocking operations, or in short anything which returns back Mono, Flux.
map should be used when you want to do the transformation of an object /data in fixed time. The operations which are done synchronously.
For ex:
return Mono.just(Person("name", "age:12"))
.map { person ->
EnhancedPerson(person, "id-set", "savedInDb")
}.flatMap { person ->
reactiveMongoDb.save(person)
}
then should be used when you want to ignore element from previous Mono and want the stream to be finised
Here's a detailed explanation from #MuratOzkan
Copy pasting the TL DR answer:
If you care about the result of the previous computation, you can use map(), flatMap() or other map variant. Otherwise, if you just want the previous stream finished, use then().
In your example, looks like your service calls do not require the input of the upstream, then you could use this instead:
Mono.just(reqObj)
.then(() -> callServiceA())
.then(() -> callServiceB())

How to call entrySet method on Mono<Map<Entity, Integer>>

Let's say that I have a method findAll which is returning Flux<Song> from ReactiveMongoRepository<Song, String>.
Below I have traditional java stream approach to find the x top-voted songs which is suggested by #Naman:
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));
Above solution works for Iterable, Set and List but the problem occurs when I pretend to call method entrySet on Mono<Map<Song, Integer>>. That's my first dive deep into Web Flux and that's why I am a little bit confused why method entrySet is not available for Mono type.
I have found a piece of code like:
Mono<Map<ByteBuffer, ByteBuffer>> serializedMap = Flux.fromIterable(() -> map.entrySet().iterator())
.collectMap(entry -> rawKey(entry.getKey()), entry -> rawValue(entry.getValue()));
but I try to avoid repacking my:
songRepository.findAll()
.collect(Collectors.toMap(Function.identity(), this::getSumOfVotes))
to Flux.fromIterable again.
I will be grateful for suggestions on what I am doing wrong or tips on how to skip this obstacle.
It's Map<Song, Integer> which has entrySet method; if you want to apply some transformation to contents of a Mono, use map:
Mono<Map<Song, Integer>> songs = ...;
Mono<Set<Map.Entry<Song, Integer>> = songs.map(Map::entrySet);
Note that map is very common name for such a method, it's available on Flux, Stream, etc.

What is the top first use case you think of, when you see the 'flatMap' method in someone else's code?

Sorry for some kind of theoretical question, but I'd like to find a way of quick reading someone else's functional code, building chain of methods use templates.
For example:
Case 1.
When I see use of .peek method or .wireTap from Spring Integration, I primarily expect logging, triggering monitoring or just transitional running external action, for instance:
.peek(params ->
log.info("creating cache configuration {} for key class \"{}\" and value class \"{}\"",
params.getName(), params.getKeyClass(), params.getValueClass()))
or
.peek(p ->
Try.run(() -> cacheService.cacheProfile(p))
.onFailure(ex ->
log.warn("Unable to cache profile: {}", ex.toString())))
or
.wireTap(sf -> sf.handle(msg -> {
monitoring.profileRequestsReceived();
log.trace("Client info request(s) received: {}", msg);
Case 2.
When I see use of .map method or .transform from Spring Integration, I understand that I'm up to get result of someFunction(input), for instance:
.map(e -> GenerateTokenRs.builder().token(e.getKey()).phoneNum(e.getValue()).build())
or
.transform(Message.class, msg -> {
ErrorResponse response = (ErrorResponse) msg.getPayload();
MessageBuilder builder = some tranforming;
return builder.build();
})
Current case.
But I don't have such a common view to .flatMap method.
Would you give me your opinion about this, please?
Add 1:
To Turamarth: I know the difference between .map and .flatMap methods. I actively use both .map, and .flatMap in my code.
But I ask community for theirs experience and coding templates.
It always helps to study the signature/javadoc of the streamish methods to understand them:
The flatMap() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.
So, typical code I expect, or wrote myself:
return someMap.values().stream().flatMap(Collection::stream)
The values of that map are sets, and I want to pull the entries of all these sets into a single stream for further processing here.
In other words: it is about "pulling out things", and getting them into a stream/collection for further processing.
I've found one more use template for .flatMap.
Let's have a look at the following code:
String s = valuesFromDb
.map(v -> v.get(k))
.getOrElse("0");
where Option<Map<String, String>> valuesFromDb = Option.of(.....).
If there's an entry k=null in the map, then we'll get null as a result of code above.
But we'd like to have "0" in this case as well.
So let's add .flatMap:
String s = valuesFromDb
.map(v -> v.get(k))
.flatMap(Option::of)
.getOrElse("0");
Regardless of having null as map's value we will get "0".

How to avoid nested forEach calls?

I have the following code:
interface Device {
// ...
boolean isDisconnected();
void reconnect();
}
interface Gateway {
// ...
List<Device> getDevices();
}
...
for (Gateway gateway : gateways) {
for(Device device : gateway.getDevices()){
if(device.isDisconnected()){
device.reconnect();
}
}
}
I want to refactor the code using Stream API. My first attempt was like the following:
gateways
.stream()
.forEach(
gateway -> {
gateway
.getDevices()
.parallelStream()
.filter(device -> device.isDisconnected())
.forEach(device -> device.reconnect())
;
}
)
;
I didn't like it so after some modifications I ended up with this code:
gateways
.parallelStream()
.map(gateway -> gateway.getDevices().parallelStream())
.map(stream -> stream.filter(device -> device.isDisconnected()))
.forEach(stream -> stream.forEach(device -> device.reconnect()))
;
My question is whether there is a way to avoid nested forEach.
You should flatten the stream of streams using flatMap instead of map:
gateways
.parallelStream()
.flatMap(gateway -> gateway.getDevices().parallelStream())
.filter(device -> device.isDisconnected())
.forEach(device -> device.reconnect());
I would improve it further by using method references instead of lambda expressions:
gateways
.parallelStream()
.map(Gateway::getDevices)
.flatMap(List::stream)
.filter(Device::isDisconnected)
.forEach(Device::reconnect);
Don't refactor your code into using Streams. You gain no benefits and gain no advantages over doing it like this, since the code is now less readable and less idiomatic for future maintainers.
By not using streams, you avoid nested forEach statements.
Remember: streams are meant to be side-effect free for safer parallelization. forEach by definition introduces side-effects. You lose the benefit of streams and lose readability at the same time, making this less desirable to do at all.
I would try this with a sequential stream before using a parallel one:
gateways
.stream()
.flatMap(gateway -> gateway.getDevices().stream())
.filter(device -> device.isDisconnected())
.forEach(device -> device.reconnect())
;
The idea is to create a stream via gateways.stream() then flatten the sequences returned from gateway.getDevices() via flatMap.
Then we apply a filter operation which acts like the if statement in your code and finally, a forEach terminal operation enabling us to invoke reconnect on each and every device passing the filter operation.
see Should I always use a parallel stream when possible?

RxJava: Return results from inner nested loops

I'm trying to use RxJava to iterate over 2 arrays and accumulate the results in the inner loop and eventually return a Single<Map>.
Here is a snippet of what I'm trying to achieve:
private Map<String, Collection<String>> processData(List<Organization> organizations, List<User> users) {
return Flowable.fromIterable(organizations) //
.flatMapSingle(organization-> Flowable.fromIterable(users) //
.filter(organization -> organization.exist(user)) //
.toMultimap(organization, user))
.blockingSingle();
}
I don't like the blockSingle call, is there a nicer way to handle this case?
EDIT
As suggested, I got rid of RxJava and use Java8 streams, as follow:
organizations.streams() //
.map(organization-> users.stream()
.filter(organization -> organization.exist(user))
.collect(MultimapCollector.toMultimap(user::role, user::id)));
The thing I can't understand is how eventually to get the result as Multimap<String,String>, currently it returns as Stream<Object>.
Why would you use RxJava for this task in the first place? There is no asynchronicity involved, so just use .filter()/.map()/.flatMap() from Java 8 (https://www.mkyong.com/java8/java-8-filter-a-map-examples/).

Categories