Java 8 code KATAs V – java.util.function Example

In this series of posts, I will be posting simple java 8 code katas.

Part I – Interface with default methods
Part II – Interface with static methods
Part III – Functional Interfaces & Lambda Expressions
Part IV – Method References
Part V – java.util.function package
Part VI – Streams

This post would be about java.util.function package.

See the example below:

FunctionExample.java


package com.idodevjobs;

import java.util.function.Function;

public class FunctionExample {
    public static void main(String[] args) {
        Function<String, String> sayHello = (String s) -> {return "hello " + s;};
        System.out.println(sayHello.apply("test"));
    }
}

The package java.util.function contains a bunch of predefined general purpose functional interfaces that can be used with lambda expressions and method references.

This example shows one of the functional interface – Function that takes one argument and returns a result.

These functional interfaces are used across JDK and can be used in our code without needing to define our own Functional Interfaces.

Advertisements
This entry was posted in java and tagged . Bookmark the permalink.

5 Responses to Java 8 code KATAs V – java.util.function Example

  1. Pingback: Java 8 code KATAs IV – Method References Example | iDoDevJobs

  2. Pingback: Java 8 code KATAs III – FunctionalInterface & Lambda Expressions Example | iDoDevJobs

  3. Pingback: Java 8 code KATAs II – Interface with static methods Example | iDoDevJobs

  4. Pingback: Java 8 code KATAs I – Interface with default methods Example | iDoDevJobs

  5. Pingback: Java 8 code KATAs VI – Streams | iDoDevJobs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s