首页 > 代码库 > Java 8 新特性:5-Supplier、IntSupplier、BinaryOperator接口

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

(原)

这个接口很简单,里面只有一个抽象方法,没有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="http://www.mamicode.com/package-summary.html">functional interface</a>
 * whose functional method is {@link #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 {@code int}-valued results.  This is the
 * {@code int}-producing primitive specialization of {@link Supplier}.
 *
 * <p>There is no requirement that a distinct result be returned each
 * time the supplier is invoked.
 *
 * <p>This is a <a href="http://www.mamicode.com/package-summary.html">functional interface</a>
 * whose functional method is {@link #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
 * {@link BiFunction} for the case where the operands and the result are all of
 * the same type.
 *
 * <p>This is a <a href="http://www.mamicode.com/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
 */
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
    /**
     * Returns a {@link BinaryOperator} which returns the lesser of two 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 two values
     * @return a {@code BinaryOperator} which returns the lesser of its operands,
     *         according to the supplied {@code 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 {@link BinaryOperator} which returns the greater of two 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 two 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> 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接口