How to use Lambda Expression in Place of Anonymous Class in Java 8 - Example Tutorial

Before Java 8, the Anonymous class was the only way you can implement functional idioms in Java. Since prior to Java 8 you cannot pass a function to another function, you would have to wrap it into an 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, the Anonymous class adds too much boilerplate code, making it hard to read and understand.

تعليقات

المشاركات الشائعة من هذه المدونة

10 points about Java Heap Space or Java Heap Memory

How HashMap works in Java

10 examples of displaytag in JSP, Struts and Spring