# functional interface

<http://tutorials.jenkov.com/java-functional-programming/functional-interfaces.html>

<https://www.baeldung.com/java-8-functional-interfaces>

## Java Functional Interfaces

A functional interface in Java is an interface that contains only one single abstract(unimplemented) method. A functional interface can contain default and static methods which do have an implementation, in addition to the single unimplemented method.

### 1. Functional Interfaces Can Be Implemented by a Lambda Expression

### 2. Built-in Functional Interfaces in Java

Java contains a set of functional interfaces designed for commonly occuring use cases.

### 2.1 Function

### 2.2 Predicate

The Java `Predicate`interface, `java.util.function.Predicate`, represents a simple function that takes a single value as parameter, and returns true or false.

eg:

```java
public interface Predicate{
    boolean test(T t);
}

public class CheckForNull implements Predicate{
    @Override
    public boolean test(Object o){
        return o!=null;
    }
}
```

```java
Predicate predicate = (value)->value!=null;
```

### 2.3 Supplier

that represents a function that supplies a value of some sorts. The *Supplier* interface can also be thought of as a factory interface.

It does not take any arguments.

eg:

```java
Supplier<Integer> supplier = ()-> new Integer((int)(Math.random()*1000D));
```

### 2.4 Consumer

that consumes a value without returning any value. A Java **Consumer** implementation could be printing out a value, or writing it to a file, or over the network etc.

eg:

```java
Consumer<Integer> consumer = (value)->System.out.println(value);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lin-4.gitbook.io/cs-notebook-java8/java-8-functional-interface.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
