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? eg:
public List<Offer> getOffersByUser(User u){
return this.shops.entrySet().stream().filter(x->x.getKey().getId()==u.getId())
.collect(List::new) //how to parameterize?
}
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.