Java8新特性

1、函数式接口(Functional Interface)@FunctionalInterface

函数式接口就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口;

可以被隐式转换为lambda表达式;

可以现有的函数友好地支持 lambda。

 

2、Lambda表达式

lambda表达式的重要特征:

  • 可选类型声明:不需要声明参数类型,编译器可以统一识别参数值。
  • 可选的参数圆括号:一个参数无需定义圆括号,但多个参数需要定义圆括号。
  • 可选的大括号:如果主体包含了一个语句,就不需要使用大括号。
  • 可选的返回关键字:如果主体只有一个表达式返回值则编译器会自动返回值,大括号需要指定明表达式返回了一个数值。

Lambda 表达式实例

Lambda 表达式的简单例子:

// 1. 不需要参数,返回值为 5  
() -> 5  
  
// 2. 接收一个参数(数字类型),返回其2倍的值  
x -> 2 * x  
  
// 3. 接受2个参数(数字),并返回他们的差值  
(x, y) -> x – y  
  
// 4. 接收2个int型整数,返回他们的和  
(int x, int y) -> x + y  
  
// 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void)  
(String s) -> System.out.print(s)
package Lambda;

public class Java8Tester {

	public static void main(String args[]) {
		Java8Tester java8Tester = new Java8Tester();

		Math m1 = (a, b) -> (int) a + (int) b;
		System.out.println(m1.operation(1, 2));
		Math m2 = (a, b) -> a + (String) b;
		System.out.println(m2.operation("hello ", "world"));
		Math m3 = (Object a, Object b) -> a.equals(b);
		System.out.println(m3.operation("hello", "hello"));
		Math.method1();
		
		Test t1 = () -> {
			System.out.println(3123);
		};
		t1.test();
		int a = 2018;
		/*
		 * 没有方法体要写大括号
		 * 正确写法:	Test t2 = () -> {};
		 * 错误写法:	Test t2 = () -> ;
		 */
		Test t2 = () -> {
			System.out.println(a);
		};
		
		/*
		 *  如果在lambda中引用了变量,不允许在其他地方修改a的值,变量默认为final
		 *  错误信息Local variable a defined in an enclosing scope must be final or effectively 
		 */
		// a = 2;
		t2.test();

	}
	
	// 函数式接口
	@FunctionalInterface
	interface Math<T> {
		
		/*
		 * 只能定义一个public abstract或默认方法
		 * 可以定义多个	static方法	和	default方法
		 */
		// T operation();
		public abstract T operation(T a, T b);

		static void method1() {
			System.out.println("static method1()");
		}

		static void method2() {
			System.out.println("static method2()");
		}

		default String method3() {
			return "default method3()";
		}

		default String method4() {
			return "default method4()";
		}
	}

	interface Test {
		void test();
	}

}

3、方法引用 ::

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

public class Java8Tester {

	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("ss");
		list.add("1");
		list.add("3123");
		list.add("31321");
		list.forEach(System.out::println);
		
		
		// 构造器引用:它的语法是Class::new,或者更一般的Class< T >::new实例如下:
		final Car car = Car.create( Car::new );
		final List< Car > cars = Arrays.asList( car );
		// 静态方法引用:它的语法是Class::static_method,实例如下:
		cars.forEach( Car::collide );
		// 特定类的任意对象的方法引用:它的语法是Class::method实例如下
		cars.forEach( Car::repair );
		// 特定对象的方法引用:它的语法是instance::method实例如下:
		final Car police = Car.create( Car::new );
		cars.forEach( police::follow );
	}
	

}


class Car {
	 //Supplier是jdk1.8的接口,这里和lamda一起使用了
   public static Car create(final Supplier<Car> supplier) {
       return supplier.get();
   }

   public static void collide(final Car car) {
       System.out.println("Collided " + car.toString());
   }

   public void follow(final Car another) {
       System.out.println("Following the " + another.toString());
   }

   public void repair() {
       System.out.println("Repaired " + this.toString());
   }
}
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.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #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();
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值