Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Filtering with condition and collecting custom Map

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());
like image 578
J-Alex Avatar asked Aug 15 '16 14:08

J-Alex


People also ask

Can we use filter and map together in Java 8?

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.

What is the difference between 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.

Which of the following functional interfaces is used in the map method?

The map(Function mapper) method takes a Function, technically speaking, an object of java. util. function. Function interface.

Which is aggregate operation in Java 8?

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.


2 Answers

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());
}
like image 169
fabian Avatar answered Oct 18 '22 20:10

fabian


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()) ;
}
like image 26
David Pérez Cabrera Avatar answered Oct 18 '22 20:10

David Pérez Cabrera