I have some collection List<Map<String, Object>>
which need to be filtered optionally with Java 8 lambda expressions.
I will receive JSON object with flags which filtering criteria have to be applied. If JSON object is not received, then no filtering is needed.
protected List<Map<String, Object>> populate(List<SomeObject> someObjects, String string) {
taskList.stream()
// How to put condition here? Ho to skip filter if no filter oprions are received?
.filter(someObject -> (if(string != null) someobject.getName == string))
// The second problem is to collect custom map like
.collect(Collectors.toMap("customField1"), someObject.getName()) ... // I need somehow put some additional custom fields here
}
Now I'm collecting custom map like that:
Map<String, Object> someMap = new LinkedHashMap<>();
someMap.put("someCustomField1", someObject.field1());
someMap.put("someCustomField2", someObject.field2());
someMap.put("someCustomField3", someObject.field3());
Once we got the Stream of Integer, we can apply maths to find out even numbers, and we passed that condition to the filter method. If we needed to filter on String, like select all string which has length > 2 then we would have called filter before map. That's all about how to use map and filter in Java 8.
Filter takes a predicate as an argument so basically you are validating your input/collection against a condition, whereas a map allows you to define or use a existing function on the stream eg you can apply String. toUpperCase(...) etc. and transform your inputlist accordingly.
The map(Function mapper) method takes a Function, technically speaking, an object of java. util. function. Function interface.
Aggregate operations − Stream supports aggregate operations like filter, map, limit, reduce, find, match, and so on. Pipelining − Most of the stream operations return stream itself so that their result can be pipelined.
Just check, whether you need to apply the filter or not and then use the filter
method or don't use it:
protected List<Map<String, Object>> populate(List<SomeObject> someObjects, String string) {
Stream<SomeObject> stream = someObjects.stream();
if (string != null) {
stream = stream.filter(s -> string.equals(s.getName()));
}
return stream.map(someObject -> {
Map<String, Object> map = new LinkedHashMap<>();
map.put("someCustomField1", someObject.Field1());
map.put("someCustomField2", someObject.Field2());
map.put("someCustomField3", someObject.Field3());
return map;
}).collect(Collectors.toList());
}
Try with this:
protected List<Map<String, Object>> populate(List<SomeObject> someObjects, String string) {
return someObjects.stream()
.filter(someObject -> string == null || string.equals(someObject.getName()))
.map(someObject ->
new HashMap<String, Object>(){{
put("someCustomField1", someObject.Field1());
put("someCustomField2", someObject.Field2());
put("someCustomField3", someObject.Field3());
}})
.collect(Collectors.toList()) ;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With