Reputation: 717
I want to know how to check and retrieve the elements of a List
inside a Map
.
private Map<User, List<Offer>> shops = new ConcurrentHashMap<>(32);
//how to write this?
public List<Offer> getOffersOlderThan(int seconds){
return this.shops.values().stream().forEach(
x->x.stream()
.filter(y-> MyTime.currentTime.minusSeconds(seconds)
.isBefore(y.getOfferBegin()))
}
I already tried to use a filter
instead the first forEach
but that didn't work either.
I want to retrieve the List
of all Offer
´s which have an Timestamp
before currentTime - seconds
.
How do I return with Java 8 an Generic List? e.g.:
public List<Offer> getOffersByUser(User u){
return this.shops.entrySet().stream().filter(x->x.getKey().getId()==u.getId())
.collect(List::new) //how to parameterize?
}
Upvotes: 0
Views: 106
Reputation: 425003
For all Offers, use flatMap()
and collect()
return shops.values().stream()
.flapMap(List::stream)
.filter(y-> MyTime.currentTime.minusSeconds(seconds).isBefore(y.getOfferBegin())
.collect(Collectors.toList());
For only the user's offers:
return shops.getOrDefault(user, Collections.emptyList())
.filter(y-> MyTime.currentTime.minusSeconds(seconds).isBefore(y.getOfferBegin())
.collect(Collectors.toList());
Upvotes: 4