Java 8 new features: 3-function interface

Source: Internet
Author: User

Original

Previously, when creating generics, it was written like this:

list<string> list =  new  arraylist<string> ();

Now, it can be written like this:

list<string> list = new arraylist<> ();

in java8 , this notation is called Diamond syntax, and some books call him the syntax of diamonds, and some call it the diamond syntax.

Look at the following example:

package com.demo.jdk8;import java.util.arraylist;import java.util.Arrays; Import Java.util.list;import Java.util.function.function;public class Test3 {public static void main (string[] args) { list<string> list = Arrays.aslist ("A", "B", "Hello"), List.stream (). Map (I-i.touppercase ()). ForEach (I- System.out.println (i)); System.out.println ("--------------------"); List.stream (). Map (string::touppercase). ForEach (I- System.out.println (i)); System.out.println ("--------------------"); Function<string, string> fun = string::touppercase; System.out.println (fun.apply ("Hello"));}}   

java8 inside, List Upper collection interface, Added a stream method

/** * Returns a sequential {@code Stream} with the this collection as its source.  * * <p>this method should is overridden when the {@link #spliterator ()} * method cannot return a spliterator That is {@code immutable}, * {@code CONCURRENT}, or <em>late-binding</em>.     (See {@link #spliterator ()} * for details.)     * This method should be overridden when the delimited iterator is not able to return an immutable, concurrent, or deferred bound value. * @implSpec * The default implementation creates a sequential {@code Stream} from the * collection ' s {@code splite     Rator}. * This default implementation creates a serial stream from Spliterator * @return a sequential {@code Stream} over the elements in this collection * @since    1.8 */Default stream<e> Stream () {return Streamsupport.stream (Spliterator (), false);  }/** * Returns A possibly Parallel {@code Stream} with this collection as its * source.     It is allowable for this method to return a sequential stream. * * <p>this method should is overridden when the {@link #sPliterator ()} * method cannot return a spliterator that is {@code immutable}, * {@code CONCURRENT}, or <em>l Ate-binding</em>.     (See {@link #spliterator ()} * for details.) * * @implSpec * The default implementation creates a parallel {@code Stream} from the * collection ' s {@code Sp     Literator}. * * @return A possibly Parallel {@code Stream} over the elements in this * collection * @since 1.8 */D Efault stream<e> Parallelstream () {return Streamsupport.stream (Spliterator (), true);}

stream< Span style= "font-family: Arial" The method returns a serial stream, parallelstream stream Span style= "font-family: Song Body", this stream is java8 The new interface, which has a map method.  

/** * Returns a stream consisting of the results of applying the given * func     tion to the elements of this stream.     * Returns a stream that contains the result of applying the given function to the element of the stream.     * <p>this is an <a href= "Package-summary.html#streamops" >intermediate * OPERATION</A>. * This is an intermediate operation * @param <R> the element type of the new stream * Returns a fresh stream * @param mapper a <a href= "pa Ckage-summary.html#noninterference ">NON-INTERFERING</A>, * <a href=" Package-summary.html#sta     Telessness ">stateless</a> * function to apply to each element * @return the new stream */<R> stream<r> map (function<? Super T,? extends R> mapper); 

Here's Map receives a parameter that is a Function Interface

@FunctionalInterfacepublic interface Function<t, r> This is a functional interface, which has an abstract method. /**     * Applies this function to the given argument.     * Apply this parameter to the given parameter     * @param t the function argument     * @return The function result *    /R apply (T T);

  The meaning of this method is to receive a stream of parameters and return a new stream.

in this example , List.stream (). Map (I-i.touppercase ()). ForEach (i, System.out.println (i));

List.stream () first converts the list to Stream,map , which is the Function interface Lambda implementation, receives a parameter for each iteration, and then converts the argument to uppercase and then back.

I-i.touppercase ()Here'sIrefers toR apply (t t);Here'sT, whilei.touppercase ()is the result of its returnR, itString::touppercaseWhat the hell is this? In theJava8This is called a reference to a method becausetoUpperCaseis notStaticmethod, then the string must be an object, so what is this object? Actually, it'sStringis the first parameter passed inT. To summarize here, this::The following method, must beLambdathe method of the first argument in an expression. Becausei-i.touppercase ()and theString::touppercaseis equivalent, in this second notation, you can also think of astringobject called thetoUpperCase,who is this object? That's the first one.I. (It's really not good to understand)

in the Function , there is also a static method:

Static <T> function<t, t> identity () {        return T, t;}

this is in Java8 The new wording introduced in the. An interface can have an implementation of a method, either default or static .

Java 8 new features: 3-function interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.