I searched for a long time but couldn't find anything in this topic which can help me in this situation. I have the following code:
return Flux.fromIterable(listOfObjects)
.parallel()
.runOn(Schedulers.parallel())
.flatMap(this::getDataResponse)
.flatMap(dataResponse-> this.afterCheckVariables(dataResponse, {currentElementOfTheList}))
.sequential();
I want to set a variable in every dataResponse based on the current element of the list. Is it possible?
If i could get the index or the current element of the list, it would be great.
Many thanks in advance
May I find a solution, if you have better approach please let me know.
The solution was the following:
I needed to create other method which gives back the current element of the List.
So my code changed like this:
return Flux.fromIterable(listOfObjects)
.parallel()
.runOn(Schedulers.parallel())
.flatMap(this::getReq)
.flatMap(requestObj -> this.getDataResponse(requestObj)
.flatMap(dataResponse-> this.afterCheckVariables(dataResponse,requestObj)))
.sequential();
The getReq method looks like this:
private Mono<ObjectOfList> getReq (ObjectOfList request){
return Mono.just(request);
}
Related
return Arrays.stream(partNumbers.get())
.filter(partNumber -> Objects.nonNull(partNumber.getDescription()))
.filter(partNumber -> partNumber.getDescription().toLowerCase().contains(rateAbbr.toLowerCase()))
.findFirst();
The above code would try to find a partNumber from a list of partNumbers where partNumber's description contains a 'rateAbbr'.
This code worked till 'rateAbbr' was a String but now it is changed to a list of rateAbbrs and I need to find a part number whose description contains any of the rateAbbrs. I tried it with streams and no luck yet. any help is appreciated.
just create a private boolean function that iterates over the list and checks if there is match, then call it inside filter method.
I would like to create a lambda replacement for this current code:
Map<String,Consumer> executionMap = new HashMap<>();
executionMap.put("operation1", str -> this.getEntity().setBooleanCondition(Boolean.parseBoolean(str))
For cases where I don't need to do transform the argument I have this:
executionMap.put("operation2", this.getEntity()::setAStringValue);
I am annoyed because I can figure out how to make the boolean case as elegant.
Additional example of being annoyed:
executionMap.put("operation3", str -> {
this.getEntity().setAStringValueA(str);
this.getEntity().setAStringValueB(str);
});
For this second case I tried :
executionMap.put("operation3",
this.getEntity()::setAStringValueA.andThen(this.getEntity()::setAStringValueB);
But this got a compilation error.
I feel like the answer(s) are obvious but I am not seeing the path.
Your operation3 is pretty straightforward.
executionMap
.put("operation3", ((Consumer<String>)this.getEntity()::setAStringValueA)
.andThen(this.getEntity()::setAStringValueB));
I am learning Java8 with stream now.
I got List of CustomDto.
CustomDto has list of CustomDto1.
CustomDto1 has list of CustomDto2.
I need result like this.
List<CustomDto> response = data from read;
response.stream()
.filter(x-> x.getCustomDto1List.stream()
.filter(y-> y.getCustomDto2List.stream()
.filter(z-> z.getCustomDto2.getSomeColumn.equals("XXX"))
)
)
Is it possible ? and If it is, How can I get this result?
Yes, you can but you should use method called anyMatch();
.filter(x-> x.getCustomDto1List.stream()
.anyMatch(y-> y.getCustomDto2List.stream()
.anyMatch(z-> z.getCustomDto2.getSomeColumn.equals("XXX"))
)
)
It looks like you're trying to find all objects which have a certain value in a nested list. Something like this?
response.stream().filter(x -> x.getCustomDto1List.stream()
.flatMap(y-> y.getCustomDto2List.stream())
.anyMatch(z-> z.getSomeColumn.equals("XXX")))
.collect(toList());
First I need to check if data is present in list then get else set default or empty value on a Java 8 stream.
Currently I am using below code without isPresent but I dont know how to use isPresent in java8.
I am trying something below which is not perfect:
String isScheme = (this.mapProgramApproaches.stream().findFirst().isPresent())? this.mapProgramApproaches.stream().findFirst().get().getIsScheme().toString() : "0";
Where as mapProgramApproaches this is set.
Don't use isPresent() (it makes no sense to run the Stream pipeline twice).
You can use map to map the value of the Optional to the required String, and then
use orElse() to return a default value when the Optional value is not present:
String isScheme = this.mapProgramApproaches.stream()
.findFirst()
.map(o->o.getIsScheme().toString())
.orElse("0");
Maybe you are looking for something like this:
String isScheme = this.mapProgramApproaches.stream()
.findFirst()
.map(p -> p.getIsScheme().toString())
.orElse("0");
I'm not sure about context in which you are doing this, but I suppose that you would like to check whether some object is scheme and then do something with that. In that case I would suggest implement it like this:
List<String> mapProgramApproaches = new ArrayList<>();
mapProgramApproaches.stream()
.filter(this::isScheme)
.findFirst()
.ifPresent(this::doYourCode)
.orElse(defaultValue);
It will make your code cleaner. And will help to avoid additional conditionals!
I am pretty new to streams.
I would like to stream the geometries EC_Geometry arraylist and if the EC_Geometry element is not present (or better equals never returns true), then I add it.
public void init(GL3 gl3, EC_Mesh mesh) {
geometries.stream()
.filter(geometry -> mesh.getGeometry().equals(geometry))
.findAny()
.orElse(..?);
}
But I am stuck at the last line
How can I solve it using streams?
Please note that equals is a method I wrote checking if the geometry is the same (i.e: if the triangles correspond)
orElse will always run even if the value returned isn't used so it is preferable to use orElseGet here which will only run if nothing is found.
geometries.stream()
.filter(geometry -> mesh.getGeometry().equals(geometry))
.findAny()
.orElseGet(() -> {
geometries.add(mesh.getGeometry());
return mesh.getGeometry();
});
.findAny().orElse(..?);
is for Optional - if you would like to get first element found.
For what you would like to achieve the best approach would be just to:
meshG = mesh.getGeometry();
if (!geometries.contains(meshG)) {
geometries.add(meshG);
}
No need to overuse Stream API.