DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Guava Functions & Java 8 Lambdas

Guava Functions & Java 8 Lambdas

Bill Bejeck user avatar by
Bill Bejeck
·
Jan. 09, 12 · Interview
Like (0)
Save
Tweet
Share
8.11K Views

Join the DZone community and get the full member experience.

Join For Free

I recently read Brian Goetz’s The State of the Lambda and after reading that article I wanted to try using Java 8 lambda expressions. In his article, Brian goes on to describe interfaces that have one method as “functional” interfaces. Functional interfaces are almost always used as anonymous classes, with the ActionListener being the canonical example. These “functional” interfaces are the initial target for lambda expressions, with the main goal being removing much of the boilerplate or ceremony around their usage. As I have been writing a series on the Guava library, the Function interface immediately came to mind as a prime candiate for trying lambda expressions. My goal is simple, take the unit test from my Guava Futures blog (it makes heavy use of the Function interface) and convert it to use lambda expressions. While I will describe the structure of the lamba expression as it pertains to the example presented, this post is not a tutorial on Java 8 lambda expressions. Rather it’s documenting my first attempts at using lambda expressions in Java.

Lambda Expressions

The first example is a test for the Futures.chain method, which takes a Function as one of it’s arguments:

Function<List<String>, ListenableFuture<List<Person>>> queryFunction =
      new Function<List<String>, ListenableFuture<List<Person>>>() {
            @Override
            public ListenableFuture<List<Person>> apply(final List<String> ids) {
                 return dbService.getPersonsByIdAsync(ids);
            }
        };

ListenableFuture<List<String>> indexSearch =
      luceneSearcher.searchAsync("firstName:martin");
ListenableFuture<List<Person>> results =
      Futures.chain(indexSearch,queryFunction,executorService);

Using lamba expressions it would now look like:

Function<List<String>, ListenableFuture<List<Person>>> queryFunction =
      ids ->(dbService.getPersonsByIdAsync(ids))

  ListenableFuture<List<String>> indexSearch =
      luceneSearcher.searchAsync("firstName:martin");
  ListenableFuture<List<Person>>results =
      Futures.chain(indexSearch, queryFunction,executorService);

Keep in mind that from the code examples above the two highlighted sections are equivalent. Let’s take a look at line 2 and explain how this matches up with lines 1-7 from the first code example

  1. ids is the input to the apply method and corresponds to the final List<String> ids parameter on line 4 in the first code example. The types of the lamba expression are being inferred from the context in which it is being used, so we don’t need to repeat them for the ids parameter.
  2. Then there is the arrow (->) token which is part of the general syntax of Java 8 lambda expressions in their current form
  3. Then we have the body of the lambda (dbService.getPersonsByIdAsync(ids)) which is a method call that returns a ListenableFuture that in turn yields a List of Person objects. Note that we did not have to put a return statement, as this is a single expression and is evaluated and returned.

The next example is a utility method from the test that returned ListenableFutures by passing anonymous Callable instances into an ExecutorService:

private ListenableFuture<List<Person>> getPersonsByFirstNameFuture(final String firstName, final boolean error) {
return executorService.submit(new Callable<List<Person>>() {
            @Override
            public List<Person> call() throws Exception {
                startSignal.await();
                if (error) {
                    throw new RuntimeException("Ooops!");
                }
                List<String> ids = luceneSearcher.search("firstName:" + firstName);
                return dbService.getPersonsById(ids);
            }
        });
}

And here is the equivalent using a lambda expression:

private ListenableFuture<List<Person>> getPersonsByFirstNameFuture(final String firstName, final boolean error) {
 return executorService.submit(() -> {startSignal.await();
                                       if (error) {
                                        throw new RuntimeException("Ooops!");
                                       }
                                     List<String> ids = luceneSearcher.search("firstName:" + firstName);
                                     return dbService.getPersonsById(ids);
                                  });
}

In this example there are no input arguments so the expression starts with empty parentheses () on line 2. There is the -> token, but in this example, the body contains multiple statements surrounded by { …}. Since there are multiple statements an explicit return statement is needed on line 7.

Environment to Run Java 8

My current laptop is a MacBook Pro, so I needed to set up an environment to run Java 8 with lambda support. Here are the steps I took:

  1. Installed LinuxMint 12 on VirtualBox.
  2. Created a directory and shared it with the LinuxMint guest
  3. Installed the developer preview of java 8.
  4. To get the source and test source from the existing maven project I ran mvn jar:jar jar:test-jar and put the resulting jar files in the shared directory
  5. Placed all the dependencies in the shared directory (guava,lucene,h2 and junit)
  6. Re-wrote the unit test to use lambdas and ran the new test from the command line

Conclusion

Although it will be some time before Java 8 with lambda support is released, what is available in the developer preview looks promising.Thanks for your time and as always comments and suggestions are welcomed

Resources

  • State of the Lambda, Part 4
  • Java 8 developer preview
  • Source code for this post
  • Guava Project Home
  • Source Code for Guava blog series

 

From http://codingjunkie.net/guava-functions-java-8-lambdas/

Java (programming language) Google Guava unit test

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • HTTP vs Messaging for Microservices Communications
  • Spring Boot, Quarkus, or Micronaut?
  • How To Handle Secrets in Docker

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: