0

I have seen this code many places but I don't understand how we can pass string.toUpperCase as a method reference in "stream.map(-)" function.? see the below code:

List myList = Arrays.asList("india", "australia", "england"); myList.stream().map(String :: toUpperCase).sorted().forEach(System.out :: println);

My understanding is stream.map method takes java.util.Function<T,U> interface object which contains U apply(T) method. That means, any method which takes one argument and return one value is the "candidate" of method reference and that can be passed in stream.map(..) method as a method reference. But in above code string.toUpperCase method does not take any argument but only returns String value. That means Signature of toUpperCase is not matching with "U apply(T)" function.

So how the above code is working properly?

0

1 Answer 1

1

A method reference takes the treats "this" (i.e., the string that toUpperCase is called on) as the first argument.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.