DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Another Java Lambda Post: Functional Interfaces

Another Java Lambda Post: Functional Interfaces

This in-depth look at Java lambdas focus on the functional interface and the ability to have a SAM (single abstract method) in your interfaces.

Martin Farrell user avatar by
Martin Farrell
·
Feb. 07, 17 · Java Zone · Tutorial
Like (13)
Save
Tweet
14.27K Views

Join the DZone community and get the full member experience.

Join For Free

I held back writing this post because I felt the last thing the Java world needs is another Lambda post, but the main reason I blog is to improve my ability to explain technologies, so I decided to do it anyway.

The syntax of a lambda is simple:

(parameters) -> expression (parameters) -> { statements; }

Examples

Runnable r1 = () -> System.out.println("Hello World"); 

// or Streams 
List<String> winnersOfToursLessThan3500km = 
  tdfWinners
     .stream()
     .filter(d -> d.getLengthKm() < 3500) // Separate out Tours less than 3500km
     .map(Winner::getName) // Get names of winners
     .collect(toList()); // Return a list

Key points are:

  • Concise: Before Java 8, we would use anonymous classes to deliver “similar” functionality.
  • Anonymous: no explicit name.
  • Function: not associated with a particular class.
  • Argument: can be passed as an argument or stored in a variable.

Functional Interfaces

The key requirement for a lambda is that the interface it implements can only have a Single Abstract Method (SAM). However, they can have any number of default and static methods

@FunctionalInterface Annotation

@FunctionalInterface is Optional and enforces this rule by generating a compiler error:

Invalid '@FunctionalInterface' annotation; 
NotFunctionalInteface is not a functional interface

Java 8 contains a number of useful interfaces in java.util.function:

Predicate<T>   
  - boolean test(T t) 
  - True/False condition on t 
  
Consumer<T>    
  - void accept(T t)  
  - Accepts t but returns no result 
  
Function<T, R> 
  - R apply(T t)      
  - Takes t and returns an instance of R

Functional Descriptor

Describes the signature of the Functional Interface:

Predicate - t -> boolean Consumer  - t  -> void Function  - t  -> R

Accessing Class Variables

Final or “effectively final”(not changed after initialization)

final String finalString = "final string";         
String effectivelyFinalString = "effectively final string";         
Runnable r = () -> {             
  System.out.println("Hi im " + finalString);             
  System.out.println("Hi im " + effectivelyFinalString);         
};         
new Thread(r).start();

But trying to change effectivelyFinalString results stops it from compiling:

String finalString = "final string";         
String effectivelyFinalString = "effectively final string";         
Runnable r = () -> {             
  System.out.println("Hi im " + finalString);             
  System.out.println("Hi im " + effectivelyFinalString);         
};         
effectivelyFinalString = "now i wont compile";         
new Thread(r).start();

The reason this works is that the lambda is accessing a copy of the final or “effectively final” field.

Interface (computing) Java (programming language) POST (HTTP)

Published at DZone with permission of Martin Farrell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • C++ Creator Bjarne Stroustrup Interview
  • Blocking Ads on Your Network Using Raspberry Pi 3 + Fedora + Pi-hole
  • Vaadin Apps as Native Executables Using Quarkus Native
  • API Security Weekly: Issue 165

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo