Java8 新特性之 Function总结

目录

Function< T , R >

DoubleFunction< R >

DoubleToIntFunction

DoubleToLongFunction

IntFunction < R >

IntToDoubleFunction

IntToLongFunction

LongFunction < R >

LongToDoubleFunction

LongToIntFunction

ToDoubleBiFunction < T , U >

ToDoubleFunction < T >

ToIntBiFunction < T , U >

ToIntFunction < T >

ToLongBiFunction < T , U >

ToLongFunction < T >

Java8 Function实例


函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。且用@Function标签标记的接口。

Java8 新增加的函数接口在 java.util.function 包下,它包含了很多类,用来支持 Java 的函数式编程,该包中的函数式接口有:

Function< T , R >

函数产生给定数据类型的结果,并接受一个参数作为给定数据类型,T参数类型,R是结果类型。

public static void functionExamp(){
		Function<Integer, String> message = age -> "年龄:".concat(String.valueOf(age));
		System.out.print(message.apply(18));
	}

DoubleFunction< R >

只接收double数据类型的值,并以给定的数据类型返回结果

public static void doubleFunctionExample(){
		// 四舍五入转换int
		DoubleFunction<Integer> message = paramte  -> (int)Math.round(paramte);
		System.out.println(message.apply(18.56));
	}

DoubleToIntFunction

接收double数据类型的值,并以integer数据类型返回结果。

public static void doubleToIntFunctionExample(){
		DoubleToIntFunction message = f -> new Double(f).intValue();
		System.out.println(message.applyAsInt(19.23));
	}

DoubleToLongFunction

接收double数据类型的值,并以long数据类型返回结果。

public static void doubleToLongFunctionExample(){
		DoubleToLongFunction message = f -> new Double(f).longValue();
		System.out.println(message.applyAsLong(19.23));
	}

IntFunction < R >

只接收integer数据类型的值,并以给定的数据类型返回结果。

	public static void intFunctionExample(){
		IntFunction<String> message = paramter -> String.valueOf(paramter);
		System.out.println(message.apply(10));
	}

IntToDoubleFunction

接收integer数据类型的值,并以double数据类型返回结果。

public static void intToDoubleFunctionExample(){
		IntToDoubleFunction message = paramter -> paramter * 2.3;
		System.out.println(message.applyAsDouble(10));
	}

IntToLongFunction

接收integer数据类型的值,并以long数据类型返回结果。

 public static void intToLongFunctionExample(){
    	IntToLongFunction message = paramter -> paramter;
    	System.out.println(message.applyAsLong(10));
    }

LongFunction < R >

只接收long数据类型的值,并以给定的数据类型返回结果。

    public static void longFunctionExample(){
    	LongFunction<Integer> message = paramter -> new Long(paramter).intValue();
    	System.out.println(message.apply(10));
    }

LongToDoubleFunction

接收long数据类型的值,并以double数据类型返回结果。

  public static void longToDoubleExample(){
    	LongToDoubleFunction message = paramter -> paramter * paramter;
    	System.out.println(message.applyAsDouble(123));
    }

LongToIntFunction

接收integer数据类型的值,并以integer数据类型返回结果。

 public static void longToIntExample(){
    	// 类型强制转换
    	LongToIntFunction message = paramter -> (int)paramter;
    	System.out.println(message.applyAsInt(23));
    }

ToDoubleBiFunction < T , U >

接收给定数据类型的两个参数,并产生double数据类型的结果。

 public static void toDoubleBitFunctionExample(){
    	ToDoubleBiFunction<Integer, Integer> message = (k1, k2) -> k1 + k2;
    	System.out.println(message.applyAsDouble(23, 12345));
    }

ToDoubleFunction < T >

接收给定数据类型的值并产生double数据类型的结果。

 public static void toDoubleFunctionExample(){
    	ToDoubleFunction<String> message = paramter -> Double.valueOf(paramter);
    	System.out.println(message.applyAsDouble("11.23"));
    }

ToIntBiFunction < T , U >

接收两个给定数据类型的参数,产生integer数据类型的结果。

   public static void toIntBitFuctionExample(){
    	 ToIntBiFunction<Integer,Integer> message = (f1,f2) -> f1+f2;
    	 System.out.println(message.applyAsInt(1, 2));
    }

ToIntFunction < T >

接收一个给定数据类型的参数并产生integer数据类型的结果。

    public static void toIntFunctionExample(){
    	ToIntFunction<String> convert = paramter -> Integer.valueOf(paramter);
    	System.out.println(convert.applyAsInt("123456"));
    }

ToLongBiFunction < T , U >

接收两个给定数据类型的参数,产生long数据类型的结果。

   public static void toLongBitFunctionExample(){
    	 ToLongBiFunction<Double,Integer> convert = (f1,f2) -> f1.intValue() + f2;
    	 System.out.println(convert.applyAsLong(23.45, 10));
    }

ToLongFunction < T >

接收一个给定数据类型的参数并产生long数据类型的结果。

   public static void toLongFunctionExample(){
    	DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点
    	 ToLongFunction<String> convert = paramter -> Long.valueOf(paramter);
    	 System.out.println(df.format(convert.applyAsLong("11")));
    }

Java8 Function实例

package com.digipower.test;
 
import java.text.DecimalFormat;
import java.util.function.DoubleFunction;
import java.util.function.DoubleToIntFunction;
import java.util.function.DoubleToLongFunction;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.IntToDoubleFunction;
import java.util.function.IntToLongFunction;
import java.util.function.LongFunction;
import java.util.function.LongToDoubleFunction;
import java.util.function.LongToIntFunction;
import java.util.function.ToDoubleBiFunction;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntBiFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongBiFunction;
import java.util.function.ToLongFunction;
 
public class FunctionTest {
	
	public static void functionExamp(){
		Function<Integer, String> message = age -> "年龄:".concat(String.valueOf(age));
		System.out.println(message.apply(18));
	}
	
	public static void doubleFunctionExample(){
		// 四舍五入转换int
		DoubleFunction<Integer> message = paramte  -> (int)Math.round(paramte);
		System.out.println(message.apply(18.56));
	}
	
	public static void doubleToIntFunctionExample(){
		DoubleToIntFunction message = f -> new Double(f).intValue();
		System.out.println(message.applyAsInt(19.23));
	}
	
	public static void doubleToLongFunctionExample(){
		DoubleToLongFunction message = f -> new Double(f).longValue();
		System.out.println(message.applyAsLong(19.23));
	}
	
	public static void intFunctionExample(){
		IntFunction<String> message = paramter -> String.valueOf(paramter);
		System.out.println(message.apply(10));
	}
	
	public static void intToDoubleFunctionExample(){
		IntToDoubleFunction message = paramter -> paramter * 2.3;
		System.out.println(message.applyAsDouble(10));
	}
	
    public static void intToLongFunctionExample(){
    	IntToLongFunction message = paramter -> paramter;
    	System.out.println(message.applyAsLong(10));
    }
    
    public static void longFunctionExample(){
    	LongFunction<Integer> message = paramter -> new Long(paramter).intValue();
    	System.out.println(message.apply(10));
    }
    
    public static void longToDoubleExample(){
    	LongToDoubleFunction message = paramter -> paramter * paramter;
    	System.out.println(message.applyAsDouble(123));
    }
    
    public static void longToIntExample(){
    	// 类型强制转换
    	LongToIntFunction message = paramter -> (int)paramter;
    	System.out.println(message.applyAsInt(23));
    }
    
    public static void toDoubleBitFunctionExample(){
    	ToDoubleBiFunction<Integer, Integer> message = (k1, k2) -> k1 + k2;
    	System.out.println(message.applyAsDouble(23, 12345));
    }
    
    public static void toDoubleFunctionExample(){
    	ToDoubleFunction<String> message = paramter -> Double.valueOf(paramter);
    	System.out.println(message.applyAsDouble("11.23"));
    }
    
    public static void toIntBitFuctionExample(){
    	 ToIntBiFunction<Integer,Integer> message = (f1,f2) -> f1+f2;
    	 System.out.println(message.applyAsInt(1, 2));
    }
    
    public static void toIntFunctionExample(){
    	ToIntFunction<String> convert = paramter -> Integer.valueOf(paramter);
    	System.out.println(convert.applyAsInt("123456"));
    }
    
    public static void toLongBitFunctionExample(){
    	 ToLongBiFunction<Double,Integer> convert = (f1,f2) -> f1.intValue() + f2;
    	 System.out.println(convert.applyAsLong(23.45, 10));
    }
    
    public static void toLongFunctionExample(){
    	DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点
    	 ToLongFunction<String> convert = paramter -> Long.valueOf(paramter);
    	 System.out.println(df.format(convert.applyAsLong("11")));
    }
	public static void main(String[] args) {
		
		// TODO Auto-generated method stub
		functionExamp();
		doubleFunctionExample();
		doubleToIntFunctionExample();
		doubleToLongFunctionExample();
		intFunctionExample();
		intToDoubleFunctionExample();
		intToLongFunctionExample();
		longFunctionExample();
		longToDoubleExample();
		longToIntExample();
		toDoubleBitFunctionExample();
		toDoubleFunctionExample();
		toIntBitFuctionExample();
		toIntFunctionExample();
		toLongBitFunctionExample();
		toLongFunctionExample();
	}
 
}

效果截图

Java8 新特性之 Function总结_第1张图片

更多Java8 Function函数,请参考:https://www.runoob.com/java/java8-functional-interfaces.html

你可能感兴趣的:(java,python,开发语言)