Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Definition of a function interface

A Functional Interface is an Interface that has one and only abstract method, but can have multiple non-abstract methods.

Function interface characteristics

  • Functional interfaces can be implicitly converted to lambda expressions.

  • Lambda expressions and method references (actually, you can also think of Lambda expressions).

  • A functional interface is defined as follows:

@FunctionalInterface
interface FunctionInterfaceSample {
    void handle(Object parameter);
}
Copy the code

Lambda expressions can then be used to represent an implementation of this interface (note: Prior to JAVA 8, it was typically implemented using anonymous classes) :

FunctionInterfaceSample functionSample = param -> System.out.println(param);
Copy the code

Java8 previously defined functional interfaces

Functional interfaces can be lambda-friendly to existing functions, so you can use them without worrying!

  • Java.lang. Runnable: Runable thread executable operation interface

  • Java. Util. Concurrent. Callable: Callbale thread executable interface

  • Java. Security. PrivilegedAction: access controller

  • Java.util.Com parator: Sort comparison interface

  • Java.io.FileFilter: FileFilter

  • Java nio. File. PathMatcher: path to the verifier

  • Java. Lang. Reflect. InvocationHandler: dynamic proxy interface

  • Java. Beans. PropertyChangeListener: the properties attribute monitor interface

  • , etc.

New function interface added to Java8

The java.util.function package contains a number of classes to support Java functional programming. The functional interfaces in this package include:

Normal double parameter type function interface:

  1. BiConsumer<T,U> : represents an operation that takes two input parameters and returns no results

  2. BiFunction<T,U,R> : represents a method that takes two input parameters and returns a result

  3. BinaryOperator: Represents an operation that operates on two operators of the same type and returns the result of the operator of the same type

  4. BiPredicate<T,U> : represents a two-parameter Boolean value method.

Double parameter type function interface:

  1. BooleanSupplier: represents the provider of the Boolean result

  2. Consumer: Represents an operation that takes an input parameter and returns nothing

  3. Function<T,R> : takes an input argument and returns a result.

Double function interface:

  1. DoubleConsumer: Represents an operation that takes a double and returns no result.

  2. DoubleFunction: represents a method that takes a double and returns the result

  3. DoublePredicate: Represents a Boolean method that takes a double parameter

  4. DoubleSupplier: represents the provider of a double value structure

  5. DoubleToIntFunction: Takes a double and returns an int.

  6. DoubleToLongFunction: Takes a double and returns a long

  7. DoubleUnaryOperator: Takes an argument of type double and returns a value of type double.

  8. DoubleBinaryOperator: Represents an operation that operates on two double operators and returns the result of a double.

Integer type function interface:

  1. IntBinaryOperator: Accepts both arguments of type int and the return value of type int.

  2. IntConsumer: Accepts an input parameter of type int, with no return value.

  3. IntFunction: takes an input parameter of type int and returns a result.

  4. IntPredicate: Takes an int input parameter and returns the result of a Boolean value.

  5. IntSupplier: Returns an int result with no argument.

  6. IntToDoubleFunction: Takes an int and returns a double.

  7. IntToLongFunction: takes an int input and returns a long result.

  8. IntUnaryOperator: accepts a parameter of type int and the return value of type int.

Function interface of type Long:

  1. LongBinaryOperator: takes two arguments of type long and returns a value of type long.

  2. LongConsumer: Takes an input parameter of type long, with no return value.

  3. LongFunction: takes an input parameter of type long and returns a result.

  4. LongPredicate: R takes a long input parameter and returns a Boolean type result.

  5. LongSupplier: Returns a value of type long with no arguments.

  6. LongToDoubleFunction: Takes a long input and returns a double result.

  7. LongToIntFunction: takes a long input and returns an int result.

  8. LongUnaryOperator: accepts an argument of type long and returns a value of type long.

  9. ObjDoubleConsumer: takes an input parameter of type Object and type double, with no return value.

  10. ObjIntConsumer: Accepts an object and an int as input parameters, with no return value.

  11. ObjLongConsumer: Takes an input parameter of type Object and type long, with no return value.

Function interface of type Long:

  1. Predicate: Takes an input parameter and returns a Boolean result.

  2. Supplier: no argument, returns a result.

Conversion type interface

  1. ToDoubleBiFunction<T,U>

Takes two input arguments and returns a double

  1. ToDoubleFunction

Takes an input parameter and returns a result of type double

  1. ToIntBiFunction<T,U>

Takes two input arguments and returns an int.

  1. ToIntFunction

Takes an input parameter and returns a result of type int.

  1. ToLongBiFunction<T,U>

Takes two input parameters and returns a result of type long.

  1. ToLongFunction

Takes an input parameter and returns a result of type long.

  1. UnaryOperator

Accepts an argument of type T and returns a value of type T.

The function interface works with the Stream Stream

The Java 8 API has added a new abstraction called a Stream that lets you process data in a declarative way.

  • The flow of elements in the pipe is processed by intermediate operations (function interfaces, processing operations) (processing on the nodes of the pipe, such as filtering, sorting, aggregation, etc.), and finally the terminal operation obtains the results of the previous processing.
+--------------------+       +------+   +------+   +---+   +-------+
| stream of elements +-----> |filter+-> |sorted+-> |map+-> |collect|
+--------------------+       +------+   +------+   +---+   +-------+
Copy the code

Elements are objects of a specific type that form a queue. A Stream in Java does not store elements, but evaluates on demand, aggregating operations: filter, Map, reduce, find, match, sorted, and so on.

  • Pipelining: All intermediate operations return the flow object itself. These operations can be cascaded into a pipe, as in fluent style. This allows you to optimize operations, such as delay and short-circuiting.
  • Internal iteration: Previously, collections were iterated explicitly outside the collection using either Iterator or for-each. This is called external iteration. A Stream provides a means of iterating internally through a Visitor pattern.

(other Java8 apis) content tips

Java8 time and date API

  • Instant timestamp
  • Duration Duration, time difference
  • LocalDate only contains dates, such as 2018-09-24
  • LocalTime contains only the time, such as 10:32:10
  • LocalDateTime contains the date and time, such as 2018-09-24 10:32:10
  • Peroid time
  • ZoneOffset time ZoneOffset, for example, +8:00
  • ZonedDateTime Date time with time zone
  • Clock Clock that can be used to get the current timestamp
  • Java. Time. The format. DateTimeFormatter time formatted class

Java8 Base64API- tips

  • Base64 encoders have become the standard for Java class libraries. Java8 provides a set of static methods for obtaining the following three Base64 codecs:

    • Basic: The output is mapped to A set of characters A-zA-Z0-9 +/, the encoding does not add any line marks, and the output decoding only supports A-zA-Z0-9 +/.
    • URL: The output maps to A set of characters A-zA-z0-9 +_, and the output is the URL and the file.
    • MIME: Output insinuations to A MIME-friendly format. Output not more than 76 characters per line, and split with ‘\r’ followed by ‘\n’. The coded output has no line splitting at the end.
Nested classes

Methods list

Base64 instance
public static void main(String[] args){
        String str = new String(Base64.getEncoder().encode("1231231231231".getBytes()));
        System.out.println(str);
        str = new String(Base64.getDecoder().decode(str));
        System.out.println(str);
        String str2 = Base64.getEncoder().encodeToString("1231231231231".getBytes("utf-8"));
        System.out.println("Base64 encoded string :" + str2);
        String mimeEncodedString = Base64.getMimeEncoder().encodeToString(UUID.randomUUID().toString());
        System.out.println("Base64 encoded string (MIME) :" + mimeEncodedString);
}
Copy the code

Content of the reference

  • www.runoob.com/java/java8-…
  • www.runoob.com/java/java8-…
  • www.cnblogs.com/ark-blog/p/…