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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Advanced Brain-Computer Interfaces With Java
  • Simplify Java: Reducing Unnecessary Layers and Interfaces [Video]
  • Java 21 SequenceCollection: Unleash the Power of Ordered Collections
  • Projections/DTOs in Spring Data R2DBC

Trending

  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • Agentic AI for Automated Application Security and Vulnerability Management
  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  1. DZone
  2. Coding
  3. Java
  4. Function Interface- A Functional Interface in the java.util.function Package in Java 8

Function Interface- A Functional Interface in the java.util.function Package in Java 8

By 
Mohamed Sanaulla user avatar
Mohamed Sanaulla
·
Apr. 06, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
12.2K Views

Join the DZone community and get the full member experience.

Join For Free

I had previously written about functional interfaces and their usage. If you are exploring the APIs to be part of Java 8 and especially those APIs which support lambda expressions you will find few interfaces like- Function, Supplier, Consumer, Predicate and others which are all part of the java.util.function package, being used extensively. These interfaces have one abstract method, which is overridden by the lambda expression defined.

In this post I will pick Function interface to explain about it in brief and it is one of the interfaces present in java.util.function package.

Function interface has two methods:
R apply(T t) – Compute the result of applying the function to the input argument
default ‹V› Function‹T,V› – Combine with another function returning a function which performs both functions.

In this post I would like to write about the apply method, creating APIs which accept these interfaces and parameters and then invoke their corresponding methods. We will also look at how the caller of the API can pass in a lambda expression in place of an implementation of the interface. Apart from passing a lambda expression, the users of the API can also pass method references, about which I havent blogged yet.

Function interface is uses in cases where you want to encapsulate some code into a method which accepts some value as an input parameter and then returns another value after performing required operations on the input. The input parameter type and the return type of the method can either be same or different.

Lets look at an API which accepts an implementation of Function interface:

public class FunctionDemo {
 
   //API which accepts an implementation of 
   //Function interface
  static void modifyTheValue(int valueToBeOperated, 
          Function<Integer, Integer> function){
 
    int newValue = function.apply(valueToBeOperated);
    /*
     * Do some operations using the new value.
     */
    System.out.println(newValue);
  } 
}

Now lets look at the code which invokes this API:

public static void main(String[] args) {
  int incr = 20;
  int myNumber = 10;
  modifyTheValue(myNumber, val-> val + incr);
 
  myNumber = 15;
  modifyTheValue(myNumber, val-> val * 10);
  modifyTheValue(myNumber, val-> val - 100);
  modifyTheValue(myNumber, val-> "somestring".length() + val - 100);
}

You can see that the lambda expressions being created accept one parameter and return some value.

I will update soon about the various APIs which use this Function interface as a parameter. Meanwhile the complete code is:

public class FunctionDemo {
   
  public static void main(String[] args) {
    int incr = 20;
    int myNumber = 10;
    modifyTheValue(myNumber, val-> val + incr);
     
    myNumber = 15;
    modifyTheValue(myNumber, val-> val * 10);
    modifyTheValue(myNumber, val-> val - 100);
    modifyTheValue(myNumber, val-> "somestring".length() + val - 100);
  }
   
  //API which accepts an implementation of 
  //Function interface
  static void modifyTheValue(int valueToBeOperated, 
          Function<Integer, Integer> function){
    int newValue = function.apply(valueToBeOperated);
    /*
     * Do some operations using the new value.
     */
    System.out.println(newValue);
  }
   
}

and the output is:

30
150
-85
-75

In the coming posts I will try to explore the other interfaces present in java.util.function package.
Note: The above code was compiled using the JDK downloaded from here and Netbeans 8 nightly builds.


 

Interface (computing) Java (programming language)

Published at DZone with permission of Mohamed Sanaulla, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Advanced Brain-Computer Interfaces With Java
  • Simplify Java: Reducing Unnecessary Layers and Interfaces [Video]
  • Java 21 SequenceCollection: Unleash the Power of Ordered Collections
  • Projections/DTOs in Spring Data R2DBC

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!