Java 8 new features: 5-supplier, Intsupplier, Binaryoperator interface

Source: Internet
Author: User

(original)

This interface is simple, there is only one abstract method, there is no default and static method.

/* Copyright (c), Oracle and/or its affiliates. All rights reserved. * ORACLE proprietary/confidential. Use are subject to license terms. */package java.util.function;/** * Represents a supplier of results. * Represents a supply of results * <p>there is no requirement, a new or distinct result be returned every * time the supplier is Invok Ed. * There is no requirement that supplier be called to return a new or different result * <p>this is a <a href= "package-summary.html" >functional interface& Lt;/a> * whose functional method is {@link #get ()}. * This is a method for Get function interface * @param <T> the type of results supplied by this supplier * * @since 1.8 */@FunctionalInterfacepu Blic interface supplier<t> {    /**     * Gets a result.     *     * @return a result     *    /T get ();}

If you have seen the previous few, then this will be very simple. Does not receive parameters, the returned type is controlled by the Tiaogan user, which is somewhat similar to the Factory mode that does not receive parameters.

Then there is a function interface that returns the int type, called intsupplier, which is associated with it,

The way it's inside is like this.

/* * Copyright (c), the Oracle and/or its affiliates. All rights reserved. * ORACLE proprietary/confidential. Use are subject to license terms.  */package java.util.function;/** * Represents a supplier of {@code int}-valued results. This is the * {@code int}-producing primitive specialization of {@link Supplier}. * * <p>there is no requirement that a distinct result being returned each * time the supplier is invoked. * * <p>this is a <a href= "package-summary.html" >functional interface</a> * whose functional method is {@ Link #getAsInt ()}.     * * @see Supplier * @since 1.8 */@FunctionalInterfacepublic interface Intsupplier {/** * Gets a result. * * @return a result */int getasint ();} 

Intsupplier is similar to Supplier, except that it restricts the return value to type int .

as this space is too short, a jumping introduction to an interface Binaryoperator

/* Copyright (c), Oracle and/or its affiliates. All rights reserved. * ORACLE proprietary/confidential. Use are subject to license terms. */package java.util.function;import java.util.objects;import java.util.comparator;/** * Represents an operation upon  Operands of the same type, producing a result * of the same type as the operands. This was a specialization of * {@link bifunction} for the case where the operands and the result was all of * the same type . * * <p>this is a <a href= "package-summary.html" >functional interface</a> * whose functional method is {@ Link #apply (Object, Object)}.  * * @param <T> the type of the operands and result of the operator * * @see bifunction * @see Unaryoperator * @since  1.8 */@FunctionalInterfacepublic interface binaryoperator<t> extends bifunction<t,t,t> {/** * Returns     A {@link binaryoperator} which returns the lesser of the elements * According to the specified {@code Comparator}. *    * @param <T> The type of the input arguments of the comparator * @param comparator a {@code comparator} for Comparing the values * @return a {@code binaryoperator} which returns the lesser of its operands, * AC Cording to the supplied {@code Comparator} * @throws NullPointerException If the argument is null */public STA Tic <T> binaryoperator<t> Minby (comparator<? Super T> Comparator) {objects.requirenonnull (compar        Ator); Return (A, b) Comparator.compare (A, B) <= 0?    A:B;  }/** * Returns a {@link binaryoperator} which Returns the greater of both elements * according to the specified     {@code Comparator}. * * @param <T> The type of the input arguments of the comparator * @param comparator a {@code comparator} fo         R comparing the values * @return a {@code binaryoperator} which returns the greater of its operands, * According to the supplied {@code Comparator} * @throws NullPointerException If the argument is null */public static <T> binaryoperator<t> m        Axby (comparator<? Super T> Comparator) {objects.requirenonnull (Comparator); Return (A, b) Comparator.compare (A, B) >= 0?    A:B; }}

because I spent a lot of time talking about Function,bifunction, this doc does not explain. Read the source directly.

Binaryoperator<t> extends Bifunction<t,t,t>

Binaryoperator inherits from bifunction , and we know that bifunction is receiving two parameters, returning a result, and Binaryoperator has only one parameter T, then its methods apply the incoming parameter and the return result are the same type. There are two static methods in it,Minby and maxby are used for comparison. Minby compares the larger one,themaxby compares the smaller one.

Here's an example to test the results.

Package Com.demo.jdk8;import Java.util.comparator;import Java.util.function.binaryoperator;import Java.util.function.intsupplier;import Java.util.function.supplier;public class Test5 {public static void main (string[ ] args) {System.out.println ("Part1---------------------"); supplier<test5class> s = () new Test5class ("Hello", 14); System.out.println (S.get (). GetName () + "=" + S.get (). Getage ()); System.out.println ("Part2---------------------"); Intsupplier SS = () 99; System.out.println (Ss.getasint ()); System.out.println ("Part3---------------------"); System.out.println (Getmin (3,6, (A, b)); System.out.println ("Part4---------------------"); System.out.println (Getmax (3,6, (A, b));} public static int Getmin (int a, int b,comparator<integer> c) {return Binaryoperator.minby (c). Apply (A, b); public static int Getmax (int a, int b,comparator<integer> c) {return Binaryoperator.maxby (c). Apply (A, b);}} Class test5class{private String name;private int Age;publIC Test5class (String name,int age) {this.name = Name;this.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

  

Java 8 new features: 5-supplier, Intsupplier, Binaryoperator interface

Related Article

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.