I've a question on method reference. To sort the names at line 14 you've used lambda: Collections.sort(persons, (o1, o2) -> o1.getName().compareTo(o2.getName()); How can we represent it in method reference. I understood how to do it using Comparator.comparing(Person::getName) as comparing takes Function. But how can we use method reference for compareTo ? I tried the following but throws exception: Collections.sort(persons, String::compareTo); Collections.sort(persons, Person::getName);
You actually do NOT create a thread. What he did there runs in the same main thread. I used to think it was useless, but i've seen some very neat timer code that would time different "Runnable" items, that was quite useful. So there are at least some uses of "Runnable" besides feeding them into a new Thread construction.
Purists will remind people that lambda expressions are really not just a shortcut to create anonymous classes in a simple and easy-to-read manner. As you have shown, they can only implement exactly one abstract method, unlike an anonymous class. They can't have "instance initializer" code like anonymous classes can. No anonymous class is produced on disk at compile time, nor loaded at run time, nor instantiated and destroyed. For all those reasons and also the fact that it doesn't change the meaning of the "this" pointer from the surrounding code, they are distinct from anonymous class instances. While it isn't terrible at the beginning to remember "lambda expressions are an easier, lighter alternative to implementing anonymous class instances for special cases where they can work", thinking that they *are* anonymous inner classes is not really correct.
Great explanation
Really a great lecture. I had lots of confusion in lambda and now I got a great understanding.
I've a question on method reference. To sort the names at line 14 you've used lambda:
Collections.sort(persons, (o1, o2) -> o1.getName().compareTo(o2.getName());
How can we represent it in method reference. I understood how to do it using Comparator.comparing(Person::getName) as comparing takes Function. But how can we use method reference for compareTo ? I tried the following but throws exception:
Collections.sort(persons, String::compareTo);
Collections.sort(persons, Person::getName);
how on the earth can you create a thread by calling run() method ??? r1.run() , please expain
You actually do NOT create a thread. What he did there runs in the same main thread. I used to think it was useless, but i've seen some very neat timer code that would time different "Runnable" items, that was quite useful. So there are at least some uses of "Runnable" besides feeding them into a new Thread construction.
Good
where is the link?
github.com/freetipscentral/java8-lambdaExpressionExample/tree/master/LambdaExpressions/src/com/freetipscentral
Link to his github -
github.com/freetipscentral/java8-lambdaExpressionExample/tree/master/LambdaExpressions/src/com/freetipscentral
Purists will remind people that lambda expressions are really not just a shortcut to create anonymous classes in a simple and easy-to-read manner. As you have shown, they can only implement exactly one abstract method, unlike an anonymous class. They can't have "instance initializer" code like anonymous classes can. No anonymous class is produced on disk at compile time, nor loaded at run time, nor instantiated and destroyed. For all those reasons and also the fact that it doesn't change the meaning of the "this" pointer from the surrounding code, they are distinct from anonymous class instances.
While it isn't terrible at the beginning to remember "lambda expressions are an easier, lighter alternative to implementing anonymous class instances for special cases where they can work", thinking that they *are* anonymous inner classes is not really correct.