Kandy
Kandy

Reputation: 673

Internal flow of lambda expression and how its identifies type of argument

I have created class called Movie and while collections sort method I have used lambda expression.

Some thing like :

Collections.sort(movieList, (o1,o2)->{return o1.getMoviesName().compareTo(o2.getMoviesName());});

Its working fine and i am happy to see that but when I am trying to understand its structure i am unable to do that. how lambda find that o1 and o2 argumnet is type of movies because I haven't describe that o1 and o2 belong to Movie object. Please help me to understand internal flow of lambda expression. As most tutorial only gives syntax and usage info only.

Upvotes: 0

Views: 85

Answers (2)

IQbrod
IQbrod

Reputation: 2265

movieList must be typed somewhere as a List<Movie>.
So o1 and o2 type is Movie.
The compiler will then check that compareTo(Object other) is a valid reachable method from movie.getMoviesName() which I assume is a String.
String implements Comparable<String> to compare with another String.
Here's the official type declaration of Collections.sort()

public static <T> void sort(List<T> list, Comparator<? super T> c)

// In your case T = Movie resulting in
public static void sort(List<Movie> list, Comparator<? super Movie> c)
// Your Comparator will treat Movie objects

Upvotes: 1

RUARO Thibault
RUARO Thibault

Reputation: 2850

If you look into the code of the Collections.sort, and anywhere in the code of java.util, you will see that many many things are generics. Generics ensure at compile time that you are using the correct type of object.

If you were not using Lambda, your code would be similar to this (example with Strings, but the idea is the same for any object the List is dealing with):

Collections.sort(strings, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
          return 0;
    }
};

In this example, I've provided an implementation of the interface Comparator in the form of an anonymous class that the method sort needs in order to sort the input collection.

As you can see, the generics code that is defined in the internal java classes Comparator is now specific to the type of my list, in my case String. That is the role of the compiler to give you this possibility (For information, generics were added since Java 1.5, more here).

Since Java8, you can replace this hideous code with something call a lambda. You enjoy a new java feature to avoid boiler plate (i.e code that needs to be here but add complexity and hide the true meaning of your code because of technical requirements).

The equivalence with a Lambda is this:

Collections.sort(strings, (s1, s2) -> s1.compareTo(s2));

Here, even if you don't see the parameter, Java guesses, thanks to generics and your list type that you are dealing with Strings.

For information, just so you know, you could also provide a lambda by specifying the parameter type (but no so common):

Collections.sort(strings, (String s1, String s2) -> s1.compareTo(s2));

Upvotes: 1

Related Questions