java8 函数引用 -> 一种利用现有函数简单推理为函数表达式即简化的函数表达式

// ***java8 函数引用 -> 一种利用现有函数简单推理为函数表达式即简化的函数表达式(省去了参数,-> 符号,只有更简化函数表达式体),[从而生成对应接口实现类默认的抽象方法体]。***

// 函数表达式可以推理成java的匿名类,那么现有的函数就可以推理成函数表达式。只要现有的函数符合要生成的方法的签名即可(入参,返回值,函数名无关紧要)

// 参数和返回值有编译器从上下文获得。从而省去了参数。只提供方法名就可以。

// 所以(arg) -> {} 函数表达式,可以简化为利用现有的函数引用:<class or instance name>::<methodName>。

package com.github.jdk8.ebook.java8_recipes2nd_edition;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 * @author doctor
 *
 * @time 2015年2月4日 上午8:46:47
 */
public class Chapter6Code {

	public static void main(String[] args) {
		HelloType helloType = (arg) -> (System.out.println("hello " + arg));
		helloType.hello("doctor");

		Function<String, String> function = (value) -> "hello " + value;
		System.out.println(function.apply("doctor"));

		BiConsumer<String, String> biConsumer = (a, b) -> System.out.println(String.join(",", a, b));
		biConsumer.accept("BiConsumer hello", "doctor");

		BiFunction<String, String, String> biFunction = (a, b) -> String.join(",", a, b);
		System.out.println(biFunction.apply("BiFunction", "hello"));

		// BinaryOperator 是一种特殊的BiFunction,参数是相同类型(入参,返回类型)
		BinaryOperator<String> binaryOperator = (a, b) -> String.join(",", a, b);
		System.out.println(binaryOperator.apply("binaryOperator ", "hello"));

		BiPredicate<String, String> biPredicate = (a, b) -> a.equals(b);
		System.out.println("BiPredicate:" + biPredicate.test("ab", "cd"));

		BooleanSupplier booleanSupplier = () -> true;
		System.out.println("BooleanSupplier:" + booleanSupplier.getAsBoolean());

		Consumer<String> consumer = (a) -> System.out.println(a);
		consumer.accept("Consumer:" + "hello");

		List<String> collect = Stream.of("c", "dca", "cddd", "abccss", "ba", "aab").parallel().sorted(Comparator.comparing(String::length)).collect(Collectors.toList());
		collect.forEach(System.out::println);

		// ***java8 函数引用 -> 一种利用现有函数简单推理为函数表达式即简化的函数表达式(省去了参数,-> 符号,只有更简化函数表达式体),[从而生成对应接口实现类默认的抽象方法体]。***
		// 函数表达式可以推理成java的匿名类,那么现有的函数就可以推理成函数表达式。只要现有的函数符合要生成的方法的签名即可(入参,返回值,函数名无关紧要)
		// 参数和返回值有编译器从上下文获得。从而省去了参数。只提供方法名就可以。
		// 所以(arg) -> {} 函数表达式,可以简化为利用现有的函数引用:<class or instance name>::<methodName>。

		// A method reference is a
		// simplified form of a lambda expression, which specifies the class name or instance name, followed by the method to
		// be called in the following format:
		// <class or instance name>::<methodName>

		// The double colon (::) operator specifies a method reference. Since a method reference is a simplified lambda
		// method, it must implement a functional interface, and the abstract method within the interface must have the same
		// argument list and return type as the method being referenced. Any arguments are subsequently derived from the
		// context of the method reference. For instance, consider the same scenario as the solution, whereby you wanted to sort
		// an array of Player objects by calling upon the Player.compareByGoal() method to perform goal comparisons.
		// The following code could be written to enable this functionality via a lambda expression:

		Integer[] integers = { 12, 45, 1, 66, 33, 22 };
		System.out.println("lambda expression排序前:");
		Arrays.stream(integers).forEach(System.out::println);

		Arrays.sort(integers, (a, b) -> Integer.compare(a, b)); // 用lambda expression 生成匿名类 Comparator , 实现compare(T o1, T o2) 方法
		System.out.println("lambda expression 排序后:");
		Arrays.stream(integers).forEach(System.out::println);

		Integer[] integers2 = { 12, 45, 1, 66, 33, 22 };
		System.out.println("函数引用排序前:");
		Arrays.stream(integers2).forEach(System.out::println);
		Arrays.sort(integers2, Integer::compare); // 用函数引用生成匿名类,
		System.out.println("函数引用排序后:");
		Arrays.stream(integers2).forEach(System.out::println);

	}

	@FunctionalInterface
	public interface HelloType {
		void hello(String message);
	}
}
时间: 2024-08-02 17:47:30

java8 函数引用 -> 一种利用现有函数简单推理为函数表达式即简化的函数表达式的相关文章

C++ DLL导出函数的两种方法(导出序号那种方法,别人看不到函数名)

第一种就直接导出函数名如下代码: #ifdef__cplusplus #define TEXPORT extern "c" _declspec(dllexport) #dlse #define TEXPORT _declspec(dllexport) TEXPORT BOOL FUN();//这就是要导出函数 这种方法查看DLL时能看到函数名. 第二种是就导出序号如下代码: bool _stdcall fun(); 在工程右键添加新项目点模块定义文件.DEF, 在在DEF文件里写 LI

第七章、函数基础之定义函数的三种方式03

目录 第七章.函数基础之定义函数的三种方式03 一.无参函数 二.有参函数 三.空函数 第七章.函数基础之定义函数的三种方式03 一.无参函数 定义函数时参数是函数体接收外部传值的一种媒介 在函数阶段括号没有参数就是无参函数.调用时不需要传入实参 如果函数体代码逻辑不需要依赖外部传入值,必须定义成无参函数 def func(): print('hello nick') func() # hello nick 二.有参函数 在函数阶段括号有参数就是有参函数.调用时需要传入实参 如果函数体代码逻辑需

以指针和引用两种参数实现删除单链表L中所有值为X的结点的函数

下面是单链表的数据结构 typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*Linklist; 1.以指针参数实现 void delete_x_1(LNode *head,ElemType x){//head为单链表头结点,删除结点的值为x LNode *l = head; LNode *p = head->next; while(p != null){ if(p->data == x){ l->next =

函数的四种调用模式

函数的四种调用模式 1.函数模式 特征 就是一个简单的函数调用,函数名前面没有任何的引导内容 this含义 this在函数模式中表示全局对象,在浏览器中是windjow对象 2.方法模式 特征 方法一定是依附与一个对象,将函数赋值给对象的一个属性,那么就成为了方法 this含义 this在方法模式调用中表示所依附的这个对象 3.构造器调用模式 与方法模式的this的区别 由于构造函数知识给this添加成员,没有做其他事情,而方法也可以完成这个操作,就this而言,构造函数与方法没有本质区别 特征

Java:函数引用

你可以Lambda表达式去创建匿名的方法.但很多时候,这份方法可能是已经存在的,这时你可以使用函数引用.这样能够使代码结构更清晰. 我们知道,贪心算法里面,一个常见的步骤是排序.假设有下面的背包类: class Package{ private int weight; // ignore setter and getter } 我们有个背包数组 Package[] packages,如果要对其排序: Arrays.sort(packages, new Comparator<Package>()

Javascript中函数的四种调用方式

一.Javascript中函数的几个基本知识点: 1.函数的名字只是一个指向函数的指针,所以即使在不同的执行环境,即不同对象调用这个函数,这个函数指向的仍然是同一个函数. 2.函数中有两个特殊的内部属性:arguments和this. arguments主要是用来保存函数参数,arguments中的callee属性主要是用来指向拥有当前arguments的函数(理解Javascript参数中的arguments对象). 3.在ECMAScript5中规范了另一个函数属性:caller(Opera

函数的四种调用模型

函数的四种调用模式在 js 中 无论是函数, 还是方法, 还是事件, 还是构造器, ... 其本质都是函数. 只是处在不同的位子而已. 四种: 函数模式方法模式构造器模式上下文模式1. 函数模式 特征: 就是一个简单的函数调用. 函数名的前面没有任何引导内容. function foo () {} var func = function () {}; ... foo(); func(); (function (){})();this 的含义: 在 函数中 this 表示全局对象, 在浏览器中是

PHP回调函数的几种用法

文章来源:http://www.cnitblog.com/CoffeeCat/archive/2009/04/21/56541.html 前言 最近在开发一个PHP系统,为了提高系统的扩展性,我想在系统中加入类似Javascript的事件处理机制,例如:我想在一篇新闻被添加以后,我想记录一下日志,用类似Javascript的代码,应该是这样写的: function fnCallBack( $news ){     //将$news的信息记录到日志中    writeLog( $news->get

swap函数 的几种实现与比较

前沿 swap函数 用于交换 a ,b 两个数.实现方法大同小异,其中不乏有好多版本,现在就其性能和可读性略作分析.不对指出还望指正.. 探讨: 其中的函数原型是: (1) void swap(int &a,int &b);传引用 (2)void swap(int *a,int *b);传地址 下面就第二个函数原型分析,代码如下: #include <stdio.h> #include <stdlib.h> /** 使用中间变量tmp */ void swap(in