How to re-use a variable in multiple Mono operators? - java

Consider this code:
Mono.just(myVar)
.flatMap(MyClass::heavyOperation)
.flatMap(MyClass::anotherHeavyOperation)
.flatMap(res -> doSomething(res, MyClass.heavyOperation(myVar)));
I don't want to call twice MyClass.heavyOperation(myVar) with the same input for the sake of performance.
How can I reuse the result of the second operation in the fourth one?
I want to do something like this, which is forbidden:
Object myObj;
Mono.just(myVar)
.flatMap(var -> {
myObj = MyClass.heavyOperation(var);
return myObj;
})
.flatMap(MyClass::anotherHeavyOperation)
.flatMap(res -> doSomething(res, myObj));

Probably the best solution is to put everything that uses myObj in the same pipeline step.
Like this:
Mono.just(myVar)
.flatMap(MyClass::heavyOperation)
.flatMap(myObj -> MyClass.anotherHeavyOperation(myObj)
.flatMap(res -> doSomething(res, myObj)));
The step that uses myObj can in turn be de-composed into a number of smaller sub-pipelines, and the top level pipeline can also continue as normally.
This is the basis of monadic operations in functional languages!

You can create a tuple in the second flat map:
Mono.just(myVar)
.flatMap(MyClass::heavyOperation)
.flatMap(x -> Tuples.of(x, MyClass.anotherHeavyOperation(myVar))
.flatMap(res -> doSomething(res.getT2(), res.getT1()));

Consider keeping the scope:
Mono.just(myVar)
.flatMap(var -> {
Object myObj = MyClass.heavyOperation(var);
return MyClass.anotherHeavyOperation(myObj)
.flatMap(res -> doSomething(res, myObj));
});

You could save Mono to variable and then just zip it again with Mono after anotherHeavyOperation.
var heavyOperation = Mono.just(myVar)
.flatMap(MyClass::heavyOperation)
.cache();
heavyOperation
.flatMap(MyClass::anotherHeavyOperation)
.zipWith(heavyOperation, (res, ho) -> doSomething(res, ho));

Related

Java 8 Lambdas flatmapping, groupingBy and mapping to get a Map of T and List<K>

Here's what I have so far:
Map<Care, List<Correlative>> mapOf = quickSearchList
.stream()
.map(QuickSearch::getFacility)
.collect(Collectors.flatMapping(facility -> facility.getFacilityCares().stream(),
Collectors.groupingBy(FacilityCare::getCare,
Collectors.mapping(c -> {
final Facility facility = new Facility();
facility.setId(c.getFacilityId());
return Correlative.createFromFacility(facility);
}, Collectors.toList()))));
I have a list of Quick Searches to begin with. Each item in the quick search has a single facility as in:
public class QuickSearch {
Facility facility;
}
In every Facility, there's a List of FacilityCare as in:
public class Facility {
List<FacilityCare> facilityCares;
}
And finally, FacilityCare has Care property as in:
public class FacilityCare {
Care care;
}
Now, the idea is to convert a List of QuickSearch to a Map of <Care, List<Correlative>>.
The code within the mapping() function is bogus, in the example above. FacilityCare only has facilityID and not Facility entity. I want the facility object that went as param in flatMapping to be my param again in mapping() function as in:
Collectors.mapping(c -> Correlative.createFromFacility(facility))
where "facility" is the same object as the one in flatMapping.
Is there any way to achieve this? Please let me know if things need to be explained further.
Edit:
Here's a solution doesn't fully utilize Collectors.
final Map<Care, List<Correlative>> mapToHydrate = new HashMap<>();
quickSearchList
.stream()
.map(QuickSearch::getFacility)
.forEach(facility -> {
facility.getFacilityCares()
.stream()
.map(FacilityCare::getCare)
.distinct()
.forEach(care -> {
mapToHydrate.computeIfAbsent(care, care -> new ArrayList<>());
mapToHydrate.computeIfPresent(care, (c, list) -> {
list.add(Correlative.createFromFacility(facility));
return list;
});
});
});
Sometimes, streams are not the best solution. This seems to be the case, because you are losing each facility instance when going down the pipeline.
Instead, you could do it as follows:
Map<Care, List<Correlative>> mapToHydrate = new LinkedHashMap<>();
quickSearchList.forEach(q -> {
Facility facility = q.getFacility();
facility.getFacilityCares().forEach(fCare ->
mapToHydrate.computeIfAbsent(fCare.getCare(), k -> new ArrayList<>())
.add(Correlative.createFromFacility(facility)));
});
This uses the return value of Map.computeIfAbsent (which is either the newly created list of correlatives or the already present one).
It is not clear from your question why you need distinct cares before adding them to the map.
EDIT: Starting from Java 16, you might want to use Stream.mapMulti:
Map<Care, List<Correlative>> mapToHydrate = quickSearchList.stream()
.map(QuickSearch::getFacility)
.mapMulti((facility, consumer) -> facility.getFacilityCares()
.forEach(fCare -> consumer.accept(Map.entry(fCare.getCare(), facility))))
.collect(Collectors.groupingBy(
e -> e.getKey(),
Collectors.mapping(
e -> Correlative.createFromFacility(e.getValue()),
Collectors.toList())));
This is what I came up with based on the information provided. The Facility and Care are stored in a temp array to be processed later in the desired map.
Map<Care, List<Correlative>> mapOf = quickSearchList.stream()
.map(QuickSearch::getFacility)
.flatMap(facility -> facility
.getFacilityCares().stream()
.map(facCare->new Object[]{facility, facCare.getCare()}))
.collect(Collectors.groupingBy(obj->(Care)obj[1], Collectors
.mapping(obj -> Correlative.createFromFacility(
(Facility)obj[0]),
Collectors.toList())));
I prepared some simple test data and this seems to work assuming I understand the ultimate goal. For each type of care offered, it puts all the facilities that offer that care in an associated list of facilities.
Inspired by #fps answer, I was able to come up with a solution that will work for the time being (pre-Java16).
Map<Care, List<Correlative>> mapOf = quickSearchList
.stream()
.map(QuickSearch::getFacility)
.map(expandIterable())
.collect(
Collectors.flatMapping(map -> map.entrySet().stream(),
Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(entry -> Correlative.createFromFacility(entry.getValue()),
Collectors.toList()
)
)
));
}
public Function<Facility, Map<Care, Facility>> expandIterable() {
return facility -> facility.getFacilityCares()
.stream()
.map(FacilityCare::getCare)
.distinct()
.collect(Collectors.toMap(c -> c, c -> facility));
}
Basically, I added a method call that returns a Function that takes in Facility as argument and returns a Map of Care as key with Facility as value. That map is used in the collection of the previous stream.

Java 8 multiple level flatMap best implementation

I have this two code examples:
Code A:
Stream<String> aStream = firstLevelList.stream()
.flatMap(firstLevelElement -> firstLevelElement.getSecondLevelList().stream()
.flatMap(secondLevelElement -> secondLevelElement.getThirdLevelList().stream()
.map(thirdLevelElement -> thirdLevelElement.toString())));
Code B:
Stream<String> aStream = firstLevelList.stream()
.flatMap(firstLevelElement -> firstLevelElement.getSecondLevelList().stream())
.flatMap(secondLevelElement -> secondLevelElement.getThirdLevelList().stream())
.map(thirdLevelElement -> thirdLevelElement.toString());
Both have exactly the same result, which is the best implementation? Why?
Option 1
The following reads nicely and feels more common:
firstLevelList.stream()
.flatMap(firstLevelElement -> firstLevelElement.getSecondLevelList().stream())
.flatMap(secondLevelElement -> secondLevelElement.getThirdLevelList().stream())
.map(Object::toString);
It avoids the nesting as #Holger pointed out, which is important for the reader.
Option 2
Alternatively, we could use a different indentation for the nesting if it's not at odds with the rest of the code base style:
firstLevelList.stream().flatMap(
firstLevelElement -> firstLevelElement.getSecondLevelList().stream().flatMap(
secondLevelElement -> secondLevelElement.getThirdLevelList().stream().map(Object::toString)
)
);
Option 3
If it still feels too cryptic, we could extract variables or methods to name the different streams:
firstLevelList.stream().flatMap(
firstLevelElement -> firstLevelElement.getSecondLevelList().stream().flatMap(
secondLevelElement -> getThirdLevelStream(secondLevelElement)
)
);
Stream<String> getThirdLevelStream(SecondLevelElement secondLevelElement) {
return secondLevelElement.getThirdLevelList().stream().map(Object::toString);
}
Option 4
We can move these methods to the element classes respecting now the Law of Demeter:
firstLevelList.stream().flatMap(FirstLevelElement::getSecondLevelStream);
class FirstLevelElement {
Stream<String> getSecondLevelStream() {
return this.getSecondLevelList().stream().flatMap(SecondLevelElement::getThirdLevelStream);
}
Collection<SecondLevelElement> getSecondLevelList() {...}
}
class SecondLevelElement {
Stream<String> getThirdLevelStream() {
return this.getThirdLevelList().stream().map(Object::toString);
}
Collection<ThirdLevelElement> getThirdLevelList() {...}
}
This will add some value if the stream methods are reused somewhere else in the code.
Conclusion
Different arguments can be made about which of these options is better. In general, they are all perfectly fine.
I prefer method references for neatness:
Stream<String> aStream = firstLevelList.stream()
.map(FirstLevelElement::getSecondLevelList)
.flatMap(List::stream)
.map(SecondLevelElement::getThirdLevelList)
.flatMap(List::stream)
.map(Object::toString);

For loop including if to parallelStream() expression

Is there a way to parallelize this piece of code:
HashMap<String, Car> cars;
List<Car> snapshotCars = new ArrayList<>();
...
for (final Car car : cars.values()) {
if (car.isTimeInTimeline(curTime)) {
car.updateCalculatedPosition(curTime);
snapshotCars.add(car);
}
}
Update: This is what I tried before asking for assistance:
snapshotCars.addAll(cars.values().parallelStream()
.filter(c -> c.isTimeInTimeline(curTime))
.collect(Collectors.toList()));
How could I integrate this line? ->
car.updateCalculatedPosition(curTime);
Well, assuming that updateCalculatedPosition does not affect state outside of the Car object on which it runs, it may be safe enough to use peek for this:
List<Car> snapshotCars = cars.values()
.parallelStream()
.filter(c -> c.isTimeInTimeline(curTime))
.peek(c -> c.updateCalculatedPosition(curTime))
.collect(Collectors.toCollection(ArrayList::new));
I say this is "safe enough" because the collect dictates which elements will be peeked by peek, and these will necessarily be all the items that passed the filter. However, read this answer for the reason why peek should generally be avoided for "significant" operations.
Your peek-free alternative is to first, filter and collect, and then update using the finished collection:
List<Car> snapshotCars = cars.values()
.parallelStream()
.filter(c -> c.isTimeInTimeline(curTime))
.collect(Collectors.toCollection(ArrayList::new));
snapShotCars.parallelStream()
.forEach(c -> c.updateCalculatedPosition(curTime));
This is safer from an API point of view, but less parallel - you only start updating the positions after you have finished filtering and collecting.
If you want parallelized access to a List you might want to use Collections.synchonizedList to get a thread-safe list:
List<Car> snapshotCars = Collections.synchronizedList(new ArrayList<>());
Then you can use the stream API like so:
cars.values()
.parallelStream()
.filter(car -> car.isTimeInTimeline(curTime))
.forEach(car -> {
car.updateCalculatedPosition(curTime);
snapshotCars.add(car);
});
In addition to RealSkeptic’s answer, you can alternatively use your own collector:
List<Car> snapshotCars = cars.values().parallelStream()
.filter(c -> c.isTimeInTimeline(curTime))
.collect(ArrayList::new,
(l,c) -> { c.updateCalculatedPosition(curTime); l.add(c); },
List::addAll);
Note that .collect(Collectors.toList()) is equivalent (though not necessarily identical) to .collect(Collectors.toCollection(ArrayList::new)) which is equivalent to .collect(ArrayList::new, List::add, List::addAll).
So our custom collector does a similar operation, but replaces the accumulator with a function, which also performs the desired additional operation.

RxJava filter and emit other items

Its possible to filter and continue emiting itens like below ?
My code that calls subscriber 2 times:
Observable<Map.Entry<String, ArrayList<MockOverview>>> requestEntries =
this.requestView.request(request)
.map(HashMap::entrySet)
.flatMapIterable(entries -> entries);
requestEntries.filter(entry -> entry.getKey().equals("featured"))
.map((Func1<Map.Entry<String, ArrayList<MockOverview>>, List<MockOverview>>) Map.Entry::getValue)
.subscribe(mockOverviews -> {
Log.i("subscrive", "featured");
});
requestEntries.filter(entry -> entry.getKey().equals("done"))
.map((Func1<Map.Entry<String, ArrayList<MockOverview>>, List<MockOverview>>) Map.Entry::getValue)
.subscribe(mockOverviews -> {
Log.i("subscrive", "featured");
});
What i want:
requestEntries.filter(entry -> entry.getKey().equals("featured"))
.map((Func1<Map.Entry<String, ArrayList<MockOverview>>, List<MockOverview>>) Map.Entry::getValue)
.subscribe(mockOverviews -> {
})
.filter(entry -> entry.getKey().equals("done"))
.map((Func1<Map.Entry<String, ArrayList<MockOverview>>, List<MockOverview>>) Map.Entry::getValue)
.subscribe(mockOverviews -> {
});
By the looks of things your second version is not equal to your first: the former looks at the requestEntries stream twice, filters on featured and done keys respectively and does its own things with it. Your second version however first filters on featured first then does some transformations and side-effects and then filter out the done. However, that Observable<entryset> is not at all in scope in that second filter lambda.
What you need to do here is use publish(<lambda>) on requestEntries and in the lambda do the stuff from your first version, use onNext instead of subscribe, merge the streams and return that combined stream. Then outside of the publish you subscribe once (and do nothing in there) or go on and use the result of your stream somewhere else.
requestEntries.publish(re -> {
Observable<...> x = re.filter(...<featured>...).map(...).doOnNext(...Log.i(...));
Observable<...> y = re.filter(...<done>...).map(...).doOnNext(...Log.i(...));
return x.mergeWith(y);
})
You can use doOnNext in the place of the first subscribe()
requestEntry.filter(v -> ...)
.map(v -> ...)
.doOnNext(v -> ...)
.filter(v -> ...)
.map(v -> ...)
.subscribe(...)
or use publish(Func1):
requestEntry.filter(v -> ...)
.map(v -> ...)
.publish(o -> {
o.subscribe(...);
return o;
})
.filter(v -> ...)
.map(v -> ...)
.subscribe(...)

How to remove an element of a HashMap whilst streaming (lambda)

I have the following situation where I need to remove an element from a stream.
map.entrySet().stream().filter(t -> t.getValue().equals("0")).
forEach(t -> map.remove(t.getKey()));
in pre Java 8 code one would remove from the iterator - what's the best way to deal with this situation here?
map.entrySet().removeIf(entry -> entry.getValue().equals("0"));
You can't do it with streams, but you can do it with the other new methods.
EDIT: even better:
map.values().removeAll(Collections.singleton("0"));
If you want to remove the entire key, then use:
myMap.entrySet().removeIf(map -> map.getValue().containsValue("0"));
I think it's not possible (or deffinitelly shouldn't be done) due to Streams' desire to have Non-iterference, as described here
If you think about streams as your functional programming constructs leaked into Java, then think about the objects that support them as their Functional counterparts and in functional programming you operate on immutable objects
And for the best way to deal with this is to use filter just like you did
1st time replying. Ran across this thread and thought to update if others are searching. Using streams you can return a filtered map<> or whatever you like really.
#Test
public void test() {
Map<String,String> map1 = new HashMap<>();
map1.put("dan", "good");
map1.put("Jess", "Good");
map1.put("Jaxon", "Bad");
map1.put("Maggie", "Great");
map1.put("Allie", "Bad");
System.out.println("\nFilter on key ...");
Map<String,String> map2 = map1.entrySet().stream().filter(x ->
x.getKey().startsWith("J"))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
map2.entrySet()
.forEach(s -> System.out.println(s));
System.out.println("\nFilter on value ...");
map1.entrySet().stream()
.filter(x -> !x.getValue().equalsIgnoreCase("bad"))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()))
.entrySet().stream()
.forEach(s -> System.out.println(s));
}
------- output -------
Filter on key ...
Jaxon=Bad
Jess=Good
Filter on value ...
dan=good
Jess=Good
Maggie=Great

Categories

Resources