[Java 8] – Lambdas expressions

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());

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s