any help to understand the generics?

hey guys. can smb explain generics to me? i know how they work in theory and i can for example write my own generic method, but when it comes to reading already written generic methods, im lost and i cant understand a thing. i have this code:
public interface Stream<T> extends BaseStream<T, Stream<T>> {
Stream<T> filter(Predicate<? super T> predicate);
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
//other methods
public interface Stream<T> extends BaseStream<T, Stream<T>> {
Stream<T> filter(Predicate<? super T> predicate);
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
//other methods
and i have the following questions: 1. why does map function has <R> but filter function doesnt? 2. why does map return R and not T? filter method returns a stream of type T (i.e. objects), but map cant? thanks in advance.
3 Replies
JavaBot
JavaBot13mo ago
This post has been reserved for your question.
Hey @bambyzas! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
imp_o_rt
imp_o_rt13mo ago
1. Filter has no R because it doesn't change the stream elements; only tests them against some criteria 2. map returns a Stream of R because you can use it to convert your original T type to a new, different, type R
JavaBot
JavaBot13mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?