how to read and understand the map() method signature?
hey guys. can smb help me out? Im messing around with streams and
map()
and im trying to understand how it works. in the ide i see this
can smb explain what the f is this? as always, google is worthless and gooling raises more questins than it gives u answers.
1. as i understand, map
function returns Stream
, but what do those R
mean?
2. map
function has a parameter called mapper
right? and its of type Function<? super T, ? extends R>
?
3. how to undertand this stuff? Function<? super T, ? extends R>
thanks alot guys.68 Replies
⌛
This post has been reserved for your question.
Hey @bambyzas! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
Do you know how generics work?
yeah. you can apply generics to a method, and then it wont return/receive one specific data type. and also you can extend that generic type up and down aka upper and lower bounds
so the class is Stream<T>
hence T refers to the type of the stream
wait
theres no
Stream<T>
and map changes a Stream<T> to a Stream<U>

the class surrounding map
or interface technically
doesnt ring any bells, sorry
look at the code and scroll up
yes
so T refers to the type of the current stream
ok. so far so good
and the goal of map() is to transform a Stream of some type to another
Stream<T> to Stream<R>
wait
map function transforms mapper (not a stream) to a Stream

the method is in
Stream<T>
so it already requires a Stream<T>
it needs both a Stream<T>
and the mapper
and then it gives you a Stream<R>
but why in the parameters only mapper is mentioned? wtf. doesnt make any sense
Do you know what lambdas are?
kinda. anonymous class (or function?) that has to comply to a functinoal interface
yes
so
mapper
is a function from T
to R
it takes a T
and returns an R
um, theres no such signature

the only important thing about
Function
is the apply methodwhy apply method? why not any other method? apply method isnt mentioned here

it takes one thing and changes it to another
it takes a
T
and gives back an R
based on what logic?
that's specified in the lambda you pass
the caller can decide how that happens
wait. im completely lost now
the mapper is to be implemented by the caller
you guys are inconsistent and all over the place. i cant keep up with u
it accepts anything
T
can assign to (? super T
), returns anything that can be assigned to R
. result is a stream with elements of type R
eg with a stream of type String, T would be String. If you were to write a method that maps that string to an Integer, R would be Integer and the mapper would return a new Integer for every String in the streamso for example what does this part mean? map returns Stream of something? what is that
something
? and why is there <R> in front of a Stream?
<R>
at the start declares one generic variable to be used for this method specifically, R. Stream<R> is the return type, it's a stream with the R as component type
-> a stream containing elements of type R
what R is depends on what the mapper function returnsso just like we have <T> in this generic method, which means
this is a generic method
, we have the same stuff in this case with Stream?
exact same concept
in the image you sent the type is called T, in the case of map it's R
the name is free to choose
but your image actually demonstrates this perfectly, fromArrayToList has a generic type T, which depends on what type the array you pass in has. the same type is then returned in a List
doing that helps the java compiler understand what types are in play, to ensure type safety
ffs. finally. but why these are different letters? whats wrong with using <T> Stream <T> map ...?
T's already used
convention is single letter uppercase names, and since the type is mainly involved with deciding the return type, R for Return fit best (or so i would assume)
as mentioned, the name of generic types is entirely up to you
things like
<GenericType1> GenericType1 returnTheSame(GenericType1 val) { return val; }
are valid, but not encouragedwhy is it so difficult
but ok, this part is clear.
its pretty easy to understand once you know what type means what and how they come to be
java's generic type system is not that complex
yeah, but if you dont use generics, lambdas, streams and other advanced stuff and have only theoretical knowledge, then u can say u have no knowledge at all haha
Well... Invariance makes it more complex than it needs to be
and then moving on to this part. it means that
map
function takes a parameter mapper
of type Function<...
?
Theoretical knowledge in programming doesn't mean anything 😁
and when u dont have places/cases when u can use it, you cant learn stuff at all
yep, Function is an interface providing one method to convert a value from the first type to the second, so in this case a value of type
? super T
to ? extends R
Yes, you can pass either a lambda or any other class that implements that interface
Find a project, and just make it
Start small though
come on. where can you use lambdas or generics, if you are building a basic api? i cant think of use cases for generics, lambdas, etc
oh they come up all the time
All the time lol
A list of strings, there's your generic usage
And modifying data with streams, there's your lambdas (and more generics)
ive been working in the bank for 2 years, and i used them maybe 5 times
but u dont have to overcomplicate things if u dont need to. and thats usually not necessary
i somehow doubt that, especially modern java uses lambdas a lot
but for all i know, you could be stuck with java 5 over there
Generics and lambdas make your code clearer. If they don't you need to use them more
I mean it is a bank
i know all that theoretical stuff, but when u r using 17th java, and u get a task, start coding, and intellij suggestions do all the work for you, u are not forced to actually learn this stuff. becuase functional interfaces are already developed by previous colleagues.
in this interface, default and static methods dont count as abstract, right? so my lambda (if i will write it) and
mapper
parameter from map()
has to comply to apply
method, right?
Yep just the apply
im trying to translate everything into easier words. so this
Function<? super T, ? extends R> mapper
would mean mapper that takes one data type and returns another (i.e. complies to
apply method
?;Yup
and how do i know if smth complies to
apply
method? bc this kinda complies too:
Well, java is pretty clever about it and does some adaptation for you so it fits
Little bit
it technucally does, but primitives dont work with the generics system. you can imitate primitives with the wrapper types tho, those work
u mean autoboxing?
okay. so without using intellisense and using my brain, how can i complete this filtering?
i know that map returns stream, and inside of map i need to write
stuff
that complies to apply
method from Function
interface, right? is this sequence of thoughts correct?Yeah
So for example 'f -> f.getFirstName()"
so since
Function
is a functional interface, i need/can write lambda. so it means that inside of farmerList.stream().map()
map part, ill need to have a lambda
?Yeah
Like the little one I showed
finally everything is falling into the places and it starts making sense
Or, any object as long as it matches that Function thing
💤
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.