Java8新特性

本文简单介绍一下Java 8的几个新特性:
    
    1、Lambda表达式(闭包)。
    2、方法和构造器引用。
    3、接口默认方法。
    4、Stream API(java.util.stream)。
    5、Optional类。
    6、Date API

1、Lambda表达式(闭包):
    
    Lambda允许把方法作为一个参数。
    
    示例如下:

/**
 * @author Bright Lee
 */
public class LambdaDemo {

	interface Math {
		int compute(int x, int y);
	}
	
	private static int compute(int x, int y, Math math) {
		return math.compute(x, y);
	}

	public static void main(String[] args) {
		// 定义Math的两个compute方法(addition和subtraction):
		Math addition     = (int x, int y) -> {return x + y;};
		Math subtraction  = (int x, int y) -> {return x - y;};
		
		// 将给Math定义的两个方法作为参数传递给另一个方法:
		int result1 = compute(1, 2, addition);
		int result2 = compute(1, 2, subtraction);
		
		// 输出结果:
		System.out.println("result1==========>" + result1);
		System.out.println("result2==========>" + result2);
	}

}

2、方法引用:
    
    示例如下:

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

/**
 * @author Bright Lee
 */
public class MethodReferenceDemo {
	
	private static class User {
		
		private int age = (int) (Math.random() * 10);
		
		// 构造器:
		public User() {
			System.out.println("User constructed!!!");
		}
		
		// 静态方法:
		public static void printAge(User user) {
			System.out.println("Age is " + user.age);
		}
		
		// 实例方法:
		public void print(User user) {
			System.out.println("Age is " + user.age);
		}
		
	}
	
	public static void main(String[] args) {
		List<User> list = new ArrayList<User>();
		
		for (int i = 0; i < 10; i++) {
			// 构造器引用:
			Supplier<User> userSupplier = User::new;
			User user = userSupplier.get();
			
			list.add(user);
		}

		// 静态方法引用(引用了User类的printAge方法):
		list.forEach(User::printAge);
		
		// 实例方法应用:
		Supplier<User> userSupplier = User::new;
		User user = userSupplier.get();
		list.forEach(user::print);
	}

}

3、函数式接口:
    
    函数式接口,有且只有一个抽象方法,但是可以有多个非抽象方法。
    函数式接口可以被隐式转换成lambda表达式。
    
    示例如下:

/**
 * @author Bright Lee
 */
public class FunctionalInterface {
	
	// 定义一个函数式接口:
	private static interface UserService {
		
		public void sayHello(String anotherUserName);
		
	}

	public static void main(String[] args) {
		
		// 实现函数式接口:
		UserService userService = anotherUserName -> {
			System.out.println("Hello " + anotherUserName + "!");
		};
		userService.sayHello("张三");
		
	}

}

4、接口默认方法:
    
    从Java 8开始,允许接口中定义方法实现,只要在方法前加上“default”修饰符就可以了。
    另外,Java 8也允许在接口中声明已实现的静态方法。
    
    示例如下:

/**
 * @author Bright Lee
 */
public class InterfaceDefaultMethodDemo {
	
	private static interface Person {
		
		// 默认方法:
		default void printInfo() {
			System.out.println("我是一个人!");
		}
		
	}
	
	private static class User implements Person {
		
		public void printInfo() {
			// 调用接口中的默认方法:
			Person.super.printInfo();
			System.out.println("我是一个用户!");
		}
		
	}
	
	public static void main(String[] args) {
		Person p = new User();
		p.printInfo();
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值