1. 程式人生 > >Java 8 新特性:5-Supplier、IntSupplier、BinaryOperator接口

Java 8 新特性:5-Supplier、IntSupplier、BinaryOperator接口

point except java 8 htm import void int() uci cti

(原)

這個接口很簡單,裏面只有一個抽象方法,沒有default和靜態方法。

/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package java.util.function;

/**
 * Represents a supplier of results.
 *代表了一個供應的結果
 * <p>There is no requirement that a new or distinct result be returned each
 * time the supplier is invoked.
 * 沒有要求supplier 被調用以後都需要返回一個新的或者不同的結果
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is [email protected]
/* */ #get()}. *這是一個方法為get的函數式接口 * @param <T> the type of results supplied by this supplier * * @since 1.8 */ @FunctionalInterface public interface Supplier<T> { /** * Gets a result. * * @return a result */ T get(); }

如果前面幾篇看過了,那麽這個會很簡單。不接收參數,返回的類型隨調用者控制,這個有點類似於不接收參數的工廠模式。

然後有一個與之相關的,返回int類型的函數式接口,叫IntSupplier

它裏面的方法是這樣的。

/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package java.util.function;

/**
 * Represents a supplier of [email protected] int}-valued results.  This is the
 * [email protected]
/* */ int}-producing primitive specialization of [email protected] Supplier}. * * <p>There is no requirement that a distinct result be returned each * time the supplier is invoked. * * <p>This is a <a href="package-summary.html">functional interface</a> * whose functional method is [email protected] #getAsInt()}. * * @see Supplier * @since 1.8 */ @FunctionalInterface public interface IntSupplier { /** * Gets a result. * * @return a result */ int getAsInt(); }

IntSupplier Supplier差不多,只不過它限定了返回值為int類型。

由於這個篇幅過短,跳躍性的再介紹一個接口BinaryOperator

/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package java.util.function;

import java.util.Objects;
import java.util.Comparator;

/**
 * Represents an operation upon two operands of the same type, producing a result
 * of the same type as the operands.  This is a specialization of
 * [email protected] BiFunction} for the case where the operands and the result are all of
 * the same type.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is [email protected] #apply(Object, Object)}.
 *
 * @param <T> the type of the operands and result of the operator
 *
 * @see BiFunction
 * @see UnaryOperator
 * @since 1.8
 */
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
    /**
     * Returns a [email protected] BinaryOperator} which returns the lesser of two elements
     * according to the specified [email protected] Comparator}.
     *
     * @param <T> the type of the input arguments of the comparator
     * @param comparator a [email protected] Comparator} for comparing the two values
     * @return a [email protected] BinaryOperator} which returns the lesser of its operands,
     *         according to the supplied [email protected] Comparator}
     * @throws NullPointerException if the argument is null
     */
    public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
    }

    /**
     * Returns a [email protected] BinaryOperator} which returns the greater of two elements
     * according to the specified [email protected] Comparator}.
     *
     * @param <T> the type of the input arguments of the comparator
     * @param comparator a [email protected] Comparator} for comparing the two values
     * @return a [email protected] BinaryOperator} which returns the greater of its operands,
     *         according to the supplied [email protected] Comparator}
     * @throws NullPointerException if the argument is null
     */
    public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
    }
}

由於之前花了大篇幅講過FunctionBiFunction,這個doc就不解釋。直接看源碼。

BinaryOperator<T> extends BiFunction<T,T,T>

BinaryOperator繼承於BiFunction ,我們知道BiFunction是接收二個參數,返回一個結果,而BinaryOperator只有一個參數T,那麽它的方法apply的傳入參數和返回結果都是相同類型。它裏面有二個靜態方法,minBymaxBy用來作比較的。minBy比較出較大的一個,maxBy比較出較小的一個。

下面給個例子測試一下結果。

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) -> a - b));
		
		System.out.println("part4---------------------");
		System.out.println(getMax(3,6 , (a,b) -> 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 age) {
		this.age = age;
	}
	
}

  

Java 8 新特性:5-Supplier、IntSupplier、BinaryOperator接口