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.
Related
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);
}
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.
Can you give a suggestion(please see pic) how can I check if selectIndicator is present on one block then I should choose another one. I know how to check if that element isPresent on whole page, but I need to find if it present on particular element. In my example I have Living Room chosen, and I need to check if DVR not chosen -choose that one. Any idea how can I do it? I was trying to check this way, but no luck:
WebElement element= driver.findElementByAccessibilityId("First element").findElementByAccessibilityId("Second element");
[http://i.stack.imgur.com/F98DM.png]
If I am not getting you wrong you want to implement a self defined data structure for an appropriate solution. That could be something similar to this :
public class DVRList {
//declare components required to comprise one item
private String dvrOptionText ;
private boolean dvrOptionCheck ;
// implement setter..getter for these two
}
...in some method set the value using the logic
DVRList dvrlist = new DVRList();
WebElement parentOfBoth = driver.findElement(By.xpath("
//android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]");
String text = parentOfBoth.findElementByAccessibilityId("First element").getText();
dvrlist.setdvrOptionText(text);
if(isElement(parenOfBoth.findElementByAccessibilityId("Second element"))
dvrlist.setdvrOptionCheck(true);
else dvrlist.setdvrOptionCheck(false);
and thereafter you can use these parameters accordingly.
Note : Parameters and approach are generalised and should be modified for serving the exact purpose.
In my elasticsearch I want to get all the indices' name of the cluster. How can I do using java?
I search the internet but there's no much useful information.
You can definitely do it with the following simple Java code:
List<IndexMetaData> indices = client.admin().cluster()
.prepareState().get().getState()
.getMetaData().getIndices();
The list you obtain contains the details on all the indices available in your ES cluster.
You can use:
client.admin().indices().prepareGetIndex().setFeatures().get().getIndices();
Use setFeatures() without parameter to just get index name. Otherwise, other data, such as MAPPINGS and SETTINGS of index, will also be returned by default.
Thanks for #Val's answer. According to your method, I use it in my projects, the code is:
ClusterStateResponse response = transportClient.admin().cluster() .prepareState()
.execute().actionGet();
String[] indices=response.getState().getMetaData().getConcreteAllIndices();
This method can put all the indices name into a String array. The method works.
there's another method I think but not tried:
ImmutableOpenMap<String, MappingMetaData> mappings = node.client().admin().cluster()
.prepareState().execute().actionGet().getState().getMetaData().getIndices().
then, we can get the keys of mappings to get all the indices.
Thanks again!
I got the following variables
List<Pruefvorschrift> listP = new ArrayList<Pruefvorschrift>();
ObservableMap<TestDevice,List<Pruefvorschrift>> testDev = FXCollections.emptyObservableMap();
in one function i want to fill the testDev by using lambda expression
//first call REST service and get data
List<TestDevice> test_dev = call.getTestDevice("");
//now do a foreach to add each entry (as key) to the testDev ObservableMap with a empty List (as value)
test_dev.stream().forEach(td ->{
TestDevice t = td;
testDev.put(t, listP);
});
but all i get is a error
java.lang.UnsupportedOperationException at
java.util.AbstractMap.put(AbstractMap.java:209)
which obviously is this row
testDev.put(t, listP);
maybe i misunderstood the new stream api but i only want to fill the observable map with all the result of the call (key) and an empty List (value which will be modified later).
Any help? Thx
Whatever Map type is returned by FXCollections#emptyObservableMap
FXCollections.emptyObservableMap();
does not support the put method. You can't add anything to it. As the javadoc states
Creates and[sic] empty unmodifiable observable list.
This has nothing to do with lambda expressions or the Stream api.
just to complete here (Sotirios Delimanolis was absolute right and me so wrong :). My problem was solved by doing a correct job with the map itself
//create empty map
Map<TestDevice,List<Pruefvorschrift>> map = new HashMap<TestDevice,List<Pruefvorschrift>>();
//use this map to create the ObservableMap
ObservableMap<TestDevice,List<Pruefvorschrift>> testDev = FXCollections.observableMap(map);
And all works...Thx Sotirios