Ok, so I have a List of Maps that I want to filter then collect to another List of Maps. Every time I put that List through the process, I get a 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 you can see... In the filter method I've had use an Object because I can't get the s variable to be a Map. When I do try, I get a ClassCastException.
Edit: Ok, maybe more information is needed...
So my segments variable is like 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 I want to do is to filter out all of the Maps that have the ct as 1. The filter method is looking at that Object (because I can't get it to cast to a Map) and checking if it == to 1, and if so it does NOT return it to the stream.
Edit2: After the comments, I edited to the following:
List<Map<String, Object>> segmentsWithMoreVersions = segments.stream()
.filter(m -> ((Long) m.get("ct")) != 1L)
.collect(Collectors.toList());
And I got the following: Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.Map
filteris supposed to do. It doesn't make any sense.filteris filtering the List... What doesn't make sense?filteronsegments.stream()you need to pass it aPredicate<Map<String, Object>>. You're passing it aPredicate<Object>and trying to cast aMapto an array. Then casting that array's element to aLong. It doesn't make any sense. So, please explain what you intended it to do.Predicate<Map<String, Object>>I get aClassCastException... That is the problem.(List<?>) query.getResultList();So it could be that the list returned actually doesn't have maps in it. In which case, there's a problem somewhere else.