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
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
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
JavaBot
JavaBot14mo 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.
dan1st
dan1st14mo ago
Do you know how generics work?
i hate SQL so much its unreal
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
dan1st
dan1st14mo ago
so the class is Stream<T> hence T refers to the type of the stream
i hate SQL so much its unreal
wait theres no Stream<T>
dan1st
dan1st14mo ago
and map changes a Stream<T> to a Stream<U>
dan1st
dan1st14mo ago
the class surrounding map or interface technically
i hate SQL so much its unreal
doesnt ring any bells, sorry
dan1st
dan1st14mo ago
look at the code and scroll up
i hate SQL so much its unreal
public interface Stream<T> extends BaseStream<T, Stream<T>> {
public interface Stream<T> extends BaseStream<T, Stream<T>> {
dan1st
dan1st14mo ago
public interface Stream<T>{
<R> Stream<R> map(...);
}
public interface Stream<T>{
<R> Stream<R> map(...);
}
yes so T refers to the type of the current stream
i hate SQL so much its unreal
ok. so far so good
dan1st
dan1st14mo ago
and the goal of map() is to transform a Stream of some type to another Stream<T> to Stream<R>
i hate SQL so much its unreal
map function transforms mapper (not a stream) to a Stream
No description
dan1st
dan1st14mo ago
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>
i hate SQL so much its unreal
but why in the parameters only mapper is mentioned? wtf. doesnt make any sense
dan1st
dan1st14mo ago
Do you know what lambdas are?
i hate SQL so much its unreal
kinda. anonymous class (or function?) that has to comply to a functinoal interface
dan1st
dan1st14mo ago
yes so mapper is a function from T to R it takes a T and returns an R
i hate SQL so much its unreal
um, theres no such signature
No description
dan1st
dan1st14mo ago
the only important thing about Function is the apply method
i hate SQL so much its unreal
why apply method? why not any other method? apply method isnt mentioned here
No description
dan1st
dan1st14mo ago
it takes one thing and changes it to another it takes a T and gives back an R
i hate SQL so much its unreal
based on what logic?
dan1st
dan1st14mo ago
that's specified in the lambda you pass the caller can decide how that happens
i hate SQL so much its unreal
wait. im completely lost now
dan1st
dan1st14mo ago
Stream<Integer> someIntStream;
Stream<String> resultStream = someIntStream.map(someInt -> ""+someInt);
Stream<Integer> someIntStream;
Stream<String> resultStream = someIntStream.map(someInt -> ""+someInt);
0x150
0x15014mo ago
the mapper is to be implemented by the caller
i hate SQL so much its unreal
you guys are inconsistent and all over the place. i cant keep up with u
0x150
0x15014mo ago
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 stream
i hate SQL so much its unreal
so 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?
No description
0x150
0x15014mo ago
<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 returns
i hate SQL so much its unreal
so 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?
No description
0x150
0x15014mo ago
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
i hate SQL so much its unreal
ffs. finally. but why these are different letters? whats wrong with using <T> Stream <T> map ...?
0x150
0x15014mo ago
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 encouraged
i hate SQL so much its unreal
why is it so difficult but ok, this part is clear.
0x150
0x15014mo ago
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
i hate SQL so much its unreal
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
tjoener
tjoener14mo ago
Well... Invariance makes it more complex than it needs to be
i hate SQL so much its unreal
and then moving on to this part. it means that map function takes a parameter mapper of type Function<...?
No description
tjoener
tjoener14mo ago
Theoretical knowledge in programming doesn't mean anything 😁
i hate SQL so much its unreal
and when u dont have places/cases when u can use it, you cant learn stuff at all
0x150
0x15014mo ago
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
tjoener
tjoener14mo ago
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
i hate SQL so much its unreal
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
0x150
0x15014mo ago
oh they come up all the time
tjoener
tjoener14mo ago
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)
i hate SQL so much its unreal
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
0x150
0x15014mo ago
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
tjoener
tjoener14mo ago
Generics and lambdas make your code clearer. If they don't you need to use them more I mean it is a bank
i hate SQL so much its unreal
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.
i hate SQL so much its unreal
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?
No description
tjoener
tjoener14mo ago
Yep just the apply
i hate SQL so much its unreal
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?;
tjoener
tjoener14mo ago
Yup
i hate SQL so much its unreal
and how do i know if smth complies to apply method? bc this kinda complies too:
public double intToDouble(int number){
return number*1.0;
}
public double intToDouble(int number){
return number*1.0;
}
tjoener
tjoener14mo ago
Well, java is pretty clever about it and does some adaptation for you so it fits Little bit
0x150
0x15014mo ago
it technucally does, but primitives dont work with the generics system. you can imitate primitives with the wrapper types tho, those work
i hate SQL so much its unreal
u mean autoboxing? okay. so without using intellisense and using my brain, how can i complete this filtering?
public static void main(String[] args) {
Farmer farmer1 = new Farmer();
farmer1.setFirstName("Name1");
farmer1.setId(1);
farmer1.setLicense("license1");
farmer1.setLastName("LastName1");

Farmer farmer2 = new Farmer();
farmer2.setFirstName("Name2");
farmer2.setId(2);
farmer2.setLicense("license2");
farmer2.setLastName("LastName2");

List<Farmer> farmerList = new ArrayList<>();
farmerList.add(farmer1);
farmerList.add(farmer2);

farmerList.stream().map()
}
public static void main(String[] args) {
Farmer farmer1 = new Farmer();
farmer1.setFirstName("Name1");
farmer1.setId(1);
farmer1.setLicense("license1");
farmer1.setLastName("LastName1");

Farmer farmer2 = new Farmer();
farmer2.setFirstName("Name2");
farmer2.setId(2);
farmer2.setLicense("license2");
farmer2.setLastName("LastName2");

List<Farmer> farmerList = new ArrayList<>();
farmerList.add(farmer1);
farmerList.add(farmer2);

farmerList.stream().map()
}
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?
tjoener
tjoener14mo ago
Yeah So for example 'f -> f.getFirstName()"
i hate SQL so much its unreal
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 ?
tjoener
tjoener14mo ago
Yeah Like the little one I showed
i hate SQL so much its unreal
finally everything is falling into the places and it starts making sense
tjoener
tjoener14mo ago
Or, any object as long as it matches that Function thing
JavaBot
JavaBot14mo 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?