Reputation: 33
I have the following Map structure
{empId=1234, empName=Mike, CDetails=[{"collegeName":"Peters Stanford","collegeLoc":"UK","collegeLoc":"UK"}]}
I need to read the value collegeLoc from the above Map
I tried this way , its working , but is there any better way
myMap.entrySet().stream().filter(map -> map.getKey().equals("CDetails")).forEach(e -> {
List<Object> objsList = (List<Object>) e.getValue();
for(int i=0;i<objsList.size();i++)
{
HashMap<String,String> ltr = (HashMap<String, String>) objsList.get(i);
System.out.println(ltr.get("collegeLoc"));
}
});
Upvotes: 0
Views: 267
Reputation: 425013
CDetails
is a List
, not a Map
.
Try this:
empMap.entrySet().stream()
.map(map -> map.get("CDetails"))
.filter(Objects::nonNull)
.flatMap(List::stream)
.map(element -> ((Map)element).get("collegeLoc"))
.filter(Objects::nonNull)
.forEach(System.out::println);
Upvotes: 1