What I leaned this week?

What I leaned this week?


1 min read

Play this article

Quite an interesting week this was!! Specially for the fact that I learned new and interesting things.

Starting from Generics in Java

I have known generic classes in Java for quite some time now. But leaning about generic methods in non-generic classes was something new.

While working on a piece of code, I got the urge to know how the map method of stream api works? It was somehow taking a object of one tome and returns the object of completely different type, for me, I have had always written methods with fixed return type.. so I was curious to know how come map method was able to handle return of any type.

I looked at source code and this is what I found.

public interface Stream<T> extends BaseStream<T, Stream<T>> {

Stream class only has T type parameter hence It was confusing to se <R> in method signature of map function.

After some googling and tutorials and java docs .. I learned about generic methods in non generic classes.