Java 8 Filter and Collect of List<Map<String, Object>> -


ok, have list of maps want filter collect list of maps. every time put list through process, list of objects... help?!

list<map<string, object>> segments = (list<map<string, object>>) (list<?>) query.getresultlist();  list<map<string, object>> segmentswithmoreversions = segments.stream()             .filter((object s) -> {                 object[] ss = (object[]) s;                 if(((long) ss[2]) == 1)                 {                     return false;                 }                 return true;             })             .collect(collectors.tolist()); 

as can see... in filter method i've had use object because can't s variable map. when try, classcastexception.

edit: ok, maybe more information needed... segments variable so:

[     {'name': 'abc', 'version': 1, 'ct': 1},     {'name': 'aaa', 'version': 1, 'ct': 1},     {'name': 'bfd', 'version': 1, 'ct': 4},     {'name': 'sde', 'version': 1, 'ct': 1} ] 

what want filter out of maps have ct 1. filter method looking @ object (because can't cast map) , checking if == 1, , if not return stream.

edit2: after comments, edited following:

list<map<string, object>> segmentswithmoreversions = segments.stream()             .filter(m -> ((long) m.get("ct")) != 1l)             .collect(collectors.tolist()); 

and got following: caused by: java.lang.classcastexception: [ljava.lang.object; cannot cast java.util.map

so, based on description, think filter want this:

segments.stream()         .filter(m -> ((long) m.get("ct")) != 1l)         .collect(tolist()); 

or being explicit type of predicate:

        .filter((map<string, object> m) ->                     ((long) m.get("ct")) != 1l) 

on edit: have erroneous data in map, or you've misunderstood way it's represented. appears segments list<object[]>.

i don't have enough information fix it.

to try debugging, like:

segments.stream()         .map(arrays::tostring)         .foreach(system.out::println); 

this tell what's in if don't have documentation refer to.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -