java8新特性学习

1.Lambda表达式

Lambda是一个匿名函数,我们可以把Lambda表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁更灵活的代码。作为一种更紧凑的代码风格,使java的语言表达能力得到了提升。

Lambda 表达式的基础语法:Java8中引入了一个新的操作符"->",名为"箭头"操作符或Lambda操作符
箭头操作符将Lambda表达式拆分成两部分
左侧: Lambda表达式的参数列表
右侧: Lambda表达式中所需执行的功能,即Lambda体
语法格式一:无参数,无返回值
() -> System.out.println("Hello Lambda!")
语法格式二:有一个参数,并且无返回值
(x) -> System.out.println(x)
语法格式三:如果有一个参数,并且无返回值,小括号可以不写
x -> System.out.println(x)

语法格式四:有两个以上的参数,并且Lambda体中有多条语句,并且有返回值
        Comparator<Integer> com = (x,y) -> {
            System.out.println("函数式接口");
            return Integer.compare(x, y);
        };    
 语法格式五:若Lambda 体 中只有一条语句,return 和 大括号都可以省略不写    
         Comparator<Integer> com = (x,y) -> Integer.compare(x, y)
语法格式六:Lambda表达式的参数列表的数据类型可以省略不写,因为javaJVM编译器可以通过上下文推断出数据类             

            Comparator<Integer> com = (Integer x,Integer y) -> Integer.compare(x, y)

 Comparator<Integer> com = (x,y) -> Integer.compare(x, y)


二.Lambda表达式需要"函数式"接口的支持
     重点:函数式接口:接口中只有一个抽象方法的接口,叫做函数式接口。
     重点:函数式接口,可以使用注解@FunctionalInterface 修饰,可以检查是否是函数式接口 

 

2.函数式接口

3.方法引用和构造器引用

package lambda;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import org.junit.Test;

/**
 * 
 * 方法引用 : 若Lambda体中的方法已经被实现,我们可以使用方法引用
 * 可以理解为方法引用是Lambda 表达式的另外一种表现形式
 * 
 * 主要有3中方法格式
 * 对象 :: 实例方法名
 * 类 :: 静态方法名
 * 类 :: 实例方法名
 * 
 * 注意:
 *	Lambda表达式调用的方法中的参数类型以及返回值类型要和函数式接口中抽象方法的参数列表和返回值类型保持一致!
 *	若Lambda参数列表中的第一个参数是实例方法的调用者,而第二个参数是实例方法的参数时,可以使用ClassName::Method
 *构造器引用
 *
 *格式:
 *ClassName::new
 *	如果要调用构造器那么构造器中的参数要与函数式接口方法中的参数保持一致
 *数组引用
 *	Type::new;
 *
 * */
public class TestMethodRef {
	
	@Test
	public void test7() {
		Function<Integer, String[]> fun = (x) -> new String[x];
		String[]  strs= fun.apply(10);
		System.out.println(strs.length);
		Function<Integer,String[]> fun1 = String[]::new;
		String[] strs1 = fun1.apply(20);
		System.out.println(strs1.length);
	}
	
	
	@Test
	public void test6() {
		Function<Integer, Employee> fun = (x) -> new Employee(x);
		Function<Integer, Employee> fun1 = Employee::new;
		Employee number = fun1.apply(101);
		System.out.println(number);
		
		BiFunction<Integer, Integer, Employee> bf = Employee::new;
		Employee  emp1 = bf.apply(1000, 5000);
		System.out.println(emp1);
	}
	
	
	//无参构造函数引用
	@Test
	public void test5() {
		Supplier<Employee> sup = () -> new Employee();
		Supplier<Employee> sup1 = Employee::new;
		
	}
	
	//类::实例方法名
	@Test
	public void test4() {
		
		BiPredicate<String, String> bipred = (x,y) -> x.equals(y);
		BiPredicate<String,String> bipred1 = String::equals;
		
	}
	
	
	
	
	//类:静态方法名
	@Test
	public void test3() {
//		Comparator<Integer> com = (x,y) -> Integer.compare(x, y);
//		com.compare(1, 2);
		
		Comparator<Integer> com1 = Integer::compare;
		
	}
	
	
	
	@Test
	public void test1() {
		PrintStream pS = System.out;
		pS.println();
		Consumer<Integer> con = (x) -> pS.println(x);
		
		PrintStream pS2 = System.out;
		Consumer<Integer> con1 = pS2::println;
		
	}
	
	
	@Test
	public void test2() {
		Employee employee = new Employee();
		Supplier<String> sup = () -> employee.getName();
		String str = sup.get();
		System.out.println(str);
		
		Supplier<Integer> sup2 = employee::getAge;
		Integer num = sup2.get();
		System.out.println(num);
		
	}
	
	
}

4.Stream API

是数据渠道,用于操作数据源(集合,数组等)所生成的元素序列.

注意!

  1. Stream 自己不会存储元素.
  2. Stream 不会改变源对象.相反,他们会返回一个持有结果的新的Stream.
  3. Stream 操作是延迟执行的.这意味着他们会等到需要结果的时候才会执行.

5.接口中的默认方法是静态方法

6.新时间时期API

7.其他新特性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值