Javarevisited: How to use Lambda Expression in Place of Anonymous Class in Java 8
▼ Thursday, January 15, 2015 How to use Lambda Expression in Place of Anonymous Class in Java 8 Before Java 8, Anonymous class was the only way you can implement functional idiom in Java. Since prior to Java 8 you cannot pass function to another function, you would have to wrap it into object, as seen in Strategy Pattern . Those are also known as function objects in Java. Anonymous class was also handy to create a throw-away implementation of SAM (Single Abstract Methods) interfaces like Runnable, Callable, CommandListener or ActionListener. Despite all these goodies and flexibility Anonymous class adds too much boiler plate code, making it hard to read and understand. Now with Java 8, you have got the ability to pass function to another function in form of lambda expression, you an easily replace your Anonymous class with lambda expression in Java. Even if you don't want to do it for your older project, you shouldn't use Anonymous class anymore for the purpose described above,Read full article from Javarevisited: How to use Lambda Expression in Place of Anonymous Class in Java 8
No comments:
Post a Comment