Why should you care about this?
It’s better to programming, it resolves the problems in a easy way, it easy to understand ,it is more clear and more flexible .
Before lambida expressions :
Comparator byMyObjectAttribute = new Comparator(MyObject)(){ public int compare(MyObject o1, MyObject o2){ return o1.getMyAttribute().compareTo(o2.getMyAttribute()); } }
After :
Comparator byMyObjectAttribute = (MyObject o1, MyObject o2) -> o1.getMyAttribute().compareTo(o2.getMyAttribute());
Reminder :
- A boolean expressions :
(List list) -> list.isEmpty();
- Creating Objects :
() -> new MyObject()
- Take values for objects
(String s) -> s.length()
- Combine to values
(int a, int b) -> a*b
- Combine to objects
(MyObject o1, MyObject o2) -> o1.getMyAttribute().compareTo(o2.getMyAttribute());