I have a Collection of Strings which I want to iterate and do a DB query on each of them and collect the response of each query into a Collection of Objects. I am sure we can do this through a for loop iterator but is there a way to do it with Java8 Streams ? This is what I came up with -
static Collection<Action> getActions(Collection<String> actionIds, RequestContext rc) {
List<Collection<Action>> ac = actionIds.stream().map(str -> hashmap.get(str)).collect(Collectors.toList());
return ac.get(0);
}
Action
is a custom class. I read that I may need to do something like this - https://itsallbinary.com/java-8-create-custom-streams-collector/ .
Is this necessary ? Or any easier ways ?
If I use this .collect(toCollection(ArrayList::new))
, it gives me Collection<Collection<Action>>