Skip to main content

Java Lambda Expressions

About a 5 minute read.   


    Java SE 8 came with the ability to use some functional programming techniques with Java's object-oriented programming structure. Most notably is the introduction of Lambda expressions.

    When I first heard of Lambda's I searched the Internet for a quick explanation as to what they are. Most of the explanations I found seemed verbose and overly complicated. Which led me to put them aside until a later point when I "needed" or had extra time to learn about them. Now, that I understand them (sometimes I spend more time searching the Internet for a "quick" answer than what it would take to read a decent chapter of a book on the subject; finally I did the latter) I can't believe that I couldn't grasp it in the first place. 

    Without debating linguistics, simply put, a Lambda expression can be defined as an anonymous method. I believe that's the best description that can be provided to help a Java programmer understand them. As one would know, with Java one could create and use anonymous classes which are simply classes or objects that are not assigned to a variable. For instance:

    button.setOnAction(new EventHandler<ActionEvent>(){
      public void handle(ActionEvent event) {
        System.out.println("Button Clicked");
      }
    });

    In the above code, I provided an anonymous class, which implements the Listener Interface, as a parameter to the button's setOnAction method. The anonymous class contains a method called handle that it gets from the Listener Interface. The setOnAction method expects any class that implements the appropriate Interface because it knows it can call its handle method when the button is clicked. We don't need a reference to the class other than passing it in as a parameter to the method, so, we don't assign it to a variable, hence, why it's called anonymous.

    So, there's anonymous classes but there's not anonymous methods. Methods belong to a class or object. There's static methods which belong to all instances of the class or object. And, there's instance methods which belong to each instance of the class or object. But there's no anonymous methods, which would belong to nothing except themselves and could be used to perform general logic.

    Then came Lambdas. They are methods (or functions) that don't belong to a particular object or class and can be used to execute general logic. Therefore, they can be seen as anonymous methods. Rewriting the above code to use Lambdas would look like this:
    button.setOnAction(event -> System.out.println("Button Clicked"));

    Look how much shorter that is! And, that's the whole idea behind Lambdas is to simplify syntax. The only reason the anonymous class was needed was to call one method which contained the code that needed to be implemented. Now, with Lambdas, we don't need to write out "single purpose" anonymous classes, we can just provide an expression with the logic we need.

    I'm not going to go in detail about the syntax of Lambdas nor am I going to introduce the Stream API. The purpose of this blog post was to explain what Lambdas are, which I hope I successfully did. Please feel free to leave a comment.

Comments

Popular posts from this blog

Face detection and live filters

Live video filters are becoming a popular trend fueled by Facebook (through their purchase of Msqrd) and Snapchat incorporating the features into their apps. These filters apply images or animations to your face using face tracking software. This technology has been around for awhile but is becoming increasingly more common due to the powerful CPU's that our mobile phones now have. Google provides an API that provides face tracking abilities through the Google Play Services library called Mobile Vision. I'm going to use their API to build a basic live filter app. The end result will look something like this:


    The bounding box wraps around the detected face and the sunglasses are the filter I chose (which is just a PNG image) which are drawn over the eyes. You could use any PNG image (with alpha for the background) you want, you will just have to adjust the layout according to where the image should be displayed. As you move your head, the box and sunglasses are redrawn…

Setting Up Connection Pooling With Elastic Beanstalk

Amazon's Elastic Beanstalk is a service which automatically scales your application when needed. It uses Amazon's Elastic Compute Cloud (EC2) instances as deployable containers which when your app requires more resources more containers will be deployed. This removes the need to manually configure your EC2 instance whenever you need more connections or resources and attempts to add simplicity to the maintenance aspect of your application. So, when you get more users of your app, your app will scale accordingly.

    Unfortunately, along with the ability to scale automatically, comes less control and configuration. Things you would normally have the ability to configure to your liking, such as your server, you no longer can. Amazon attempts to address this issue with configuration files. You can provide configuration files which can set up your server. These files are either written in JSON or the horrible format YAML. Though these files provide some level of control, you ca…

Android Guitar Tuner

Recently I created a guitar tuner application for Android that is written with pure Java (no C++ or NDK usage). The design was inspired by the Google Chrome team's guitar tuner web app using the WebAudio API. I wanted to code a version written natively for Android that didn't have to rely on a WebView, the WebAudio APIs, or server-side logic. Also, I wanted this application to be available to as many versions of Android as possible (whereas the WebAudio API may only be supported in more recent versions of WebView available only on newer flavors of Android). So, I coded it from scratch. I used a portion of the open source TarsosDSP project (their YIN algorithm) to help with the pitch detection.

    The application is available in the Google Play Store for Android: https://play.google.com/store/apps/details?id=com.chrynan.guitartuner. The project is completely open source and the code can be found on the GitHub repository: https://github.com/chRyNaN/Android-Guitar-Tuner. Fi…