JDK8 新特性

Lambda 表达式

  1. 是对匿名内部类对象的一种简化
  2. java8 中引入了一个新的操作符“ -> ”,称为箭头运算符,或者Lambda 运行符
  3. 作用:就是用于分隔前后两部分
  4. 左边: 表示Lambda 表达式的参数列表(接口中,定义的抽象方法的参数)
  5. 右边:表示的是方法的方法体,Lambda体
  6. 语法格式1:没有参数,也没有返回值,方法体只有一句
  7. 语法格式2:有一个参数,也没有返回值,方法体只有一句,说明:如果只有一个参数,那么小括号可以省略
  8. 语法格式3:有多个参数,没有返回值,格式和语法格式2相同
  9. 语法格式4:接口中需要重写的方法,方法内容有多句返回值

注意事项

  1. 如果Lambdar 体中语句只有一句,那么大括号可以省略不写。
  2. 如果大括号中只有一条语句,并且return 语句,那么return 关键字也可以省略不写(如果要省略return,那么就必须省略大括号)

代码示例


public class Demo01 {
	public static void main(String[] args) {
		
		test01();
		MyInter3 my3 = new MyInter3() {
			
			@Override
			public int show(int x, int y) {
				int c = x + y;
				return c;
			}
		};
		System.out.println(my3.show(10, 20));
//		my3.show(10, 20);
		
		//lambdar 带有两个参数,直接写x + y,省略return语句。
		MyInter3 m3 = (x,y) -> x + y;
	
		System.out.println(m3.show(20, 30));
		
	}

	private static void test02() {
		new MyInter2() {
			
			@Override
			public void show(int x) {
				System.out.println("这个值为:"+x);
			}
		}.show(666);;
//		my2.show(666);
		
		//带有参数的Lambdar,带有一个参数的可以省略括号(),直接写x ->就可以。
		MyInter2 m2 = x ->System.out.println("这个值为:"+x);
		m2.show(888);
	}

	private static void test01() {
		MyInter my = new MyInter() {
			
			@Override
			public void show() {
				System.out.println("Hello World 1");
			}
			
//			@Override
//			public void print() {
//				System.out.println("Hello World 2");
//			}
		};
		my.show();
//		my.print();
		
		MyInter m = () -> System.out.println("Hello World 2 Lambdar");
		m.show();
//		m.print();
	}
}	
interface MyInter{
	public abstract void show();
//	public abstract void print();
}
interface MyInter2{
	public abstract void show(int x);
}
interface MyInter3{
	public abstract int show(int x,int y);
}

函数式接口

概述

  1. Lambda 表达式使用的前提,就是接口必须是一个函数式接口

  2. 定义:
    如果在接口中,只有一个抽象方法,那么这个接口就是函数式接口

  3. 格式说明:使用注解来检查当前接口是否是一个函数式接口
    @FunctionalInterface
    如果不是函数式接口,则编译会报错

//一个接口中只有一个方法,此接口表示一个函数式接口
@FunctionalInterface
interface MyInter{
	public abstract void show();
}
//定义两个抽象方法,不是函数式接口,编译报错
@FunctionalInterface
interface MyInter2{
	public abstract void show();
	public abstract void show2();
}
//一个方法也没有,编译也会报错,不是一个函数式接口
@FunctionalInterface
interface MyInter3{
	
}

常用内置函数式接口

  1. 说明:
    java8 中提供了一些常用的函数式接口,在使用类似功能的时候,不需要额外定义接口,直接使用jdk 中提供即可。

  2. 罗列:
    Conusmer : 消费型接口
    void accept(T t);
    Supplier : 供给型接口
    T get(t);
    Function<T ,R> : 函数式接口
    R.apply(T t);
    Predicate : 断言式接口
    boolean test(T t);

消费型接口

1.作用:这个接口可以接收一个数据,然后将这个数据进行处理。并没有返回值,仅仅是对接收的数据进行处理。

public class Demo02 {
	public static void main(String[] args) {
		//消费型接口Consumer,实现内容就是实现方法的功能
		Consumer<Integer> con = x ->System.out.println(x);
		//调用方法,将实参给传进去。
		print(666, con);
	}
	public static void print(int x,Consumer<Integer> con) {
		//把形式参数出传递给Consumer接口
		con.accept(x);
	}
}

供给型接口

  1. Supplier
  2. 名称:供给型接口
  3. 抽象方法:T get()
  4. 作用:这个接口不接受数据,没有参数。而是经过实现类重写之后,可以返回一个指定类型的数据。

代码示例

从0-20中随机抽取10个元素
public class Demo03 {
	public static void main(String[] args) {
		Random r = new Random();
		//供给型接口的实现对象,用来实现具体的方法需要的数据
		Supplier<Integer> sup = () -> r.nextInt(21);//没有传入参数,直接写()
		//调用这个方法的时候,需要将供给型接口的具体实现对象传入
		ArrayList<Integer> list = getList(sup);
		System.out.println(list);
	}
	//定义一个方法,从0-20中随机抽取10个元素
	public static ArrayList<Integer> getList(Supplier<Integer> sup){
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < 10; i++) {
			//每需要在集合中添加一个数据,我就找供给型接口要,具体怎么实现我要的数据,方法不管。
			list.add(sup.get());
		}
		return list;
	}
}

函数型接口

  1. Function<T,R>
    注:T 表示类型,R 表示返回类型
  2. 名称:函数型接口
  3. 抽象方法:R apply(T t)
  4. 作用:
    这个接口可以接收数据,实现类可以对这个接收的数据进行处理,处理完成之后,将处理的结果当做返回值进行返回。

代码示例


public class Demo05 {
	public static void main(String[] args) {
		
		Function<Integer, Integer> fun = (x) -> x*10;
		int num = getNum(10, fun);
		System.out.println(num);
	}
	写一个方法,传入一个int数据,将这个数据*10之后的结果返回给我
	public static int getNum(int x,Function<Integer, Integer> fun) {
		
		return fun.apply(x);
	}
}

断言性接口

  1. Predicate
  2. 名称:断言型接口
  3. 抽象方法:boolean test(T t)
  4. 作用:这个接口可以接收数据,实现类可以通过对这个数据进行条件判断,返回一个boolean的值
**代码示例**
public class Demo06 {
	public static void main(String[] args) {
		
		Predicate<Integer> pre = x -> x > 10;
		boolean b = getB(pre);
		System.out.println(b);
	}
	//写一个方法,键盘录入一个整数,如果这个整数大于10,就返回true,否则false
	public static boolean getB(Predicate<Integer> pre) {
		Scanner sc = new Scanner(System.in);
		int i = sc.nextInt();
		//返回方式一
		if(pre.test(i)) {
			return true;
		}else {
			return false;
		}
		//返回方式二
//		return pre.test(i);
	}
}

练习:
将方法里面的集合传入方法中,这个方法判断传入的集合里面的每一个元素是否为偶数,就添加到一个新集合中。将新的集合返回

public class Demo07 {
	public static void main(String[] args) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(111);
		list.add(222);
		list.add(333);
		list.add(444);
		list.add(123);
		list.add(-8);
		
		Predicate<Integer> pre = x ->x%2==0;
		ArrayList<Integer> evenNums = getEvenNums(list, pre);
		System.out.println(evenNums);	
	}
	/*将方法里面的集合传入方法中,这个方法判断传入的集合里面的每一个元素是否为偶数,
	就添加到一个新集合中。将新的集合返回*/
	public static ArrayList<Integer> getEvenNums(ArrayList<Integer> list,Predicate<Integer> pre) {
		ArrayList<Integer> list2 = new ArrayList<Integer>();
		for (Integer in : list) {
			if(pre.test(in)) {
				list2.add(in);
			}
		}
		return list2;
	}
}

Streaming

  1. 在jdk1.8 中,提供了一个Stream 类型,可以对下数据进行过滤
  2. 好处:比不断的自定义循环,要简单很多

Stream类型数据的获取

  1. Collection 的获取:
    调用stream()方法即可,返回Stream 类型的对象

  2. Map 的获取:不能直接获取Stream 类型,通过以下方法获取
    (1)keySet().stream()
    (2)values().stream()
    (3)entrySet().stream()

  3. 数组的获取
    Stream 中的of方法,Stream.of(数组)

public class Demo08 {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<String>();
		list.add("张无忌");
		list.add("张三丰");
		list.add("张飞");
		list.add("维斯布鲁克");
		
		/*
		 * //打印一下,集合里面首字母为张长度为3的元素
	 list.stream().filter(x -> x.startsWith("张")).filter(x -> x.length() == 3).forEach(x->System.out.println(x));
		
		 */
		
		//Collection 集合获取Stream类型对象的方式
		Stream<String> s = list.stream();
		
		//map集合获取stream类型对象的方法
		Map m = new HashMap();
		//第一种
		Stream stream = m.keySet().stream();
		//第二种
		Stream stream2 = m.entrySet().stream();
		//第三种
		Stream stream3 = m.values().stream();
		
		//获取数组stream类型对象的方式
		int[] arr = {1,2,3,4};
		Stream<int[]> of = Stream.of(arr);
		String str = "你好";
		Stream<String> of2 = Stream.of(str);
		
	}
}

Stream中常用的方法

  1. 常用方法:Stream 接口中的常用方法,用于对流中的数据进行过滤,筛选或者操作

  2. 分类:
    (1)终结方法:调用方法之后,返回值不再是Stream 类型本身,无法继续调用stream 中的方法。例如:forEach,count。
    (2)延迟方法:调用完成之后,返回值还是Stream 类型,可以继续调用Stream 中各种方法。例如:filter,map。

  3. 罗列:
    (1)Stream filter(Predicate pre)
    根据pre描述的判断条件,对流中的数据进行过滤
    (2)Stream map(Function fun)
    将流中的所有T 类型数据,都根据fun这个函数型接口,转换成其他的R 类型数据
    (3)forEach(Consumer con)
    将流中的数据,根据con 描述的处理方式进行处理。
    (4)long coun()
    返回流中的元素数

public class Demo08 {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<String>();
		list.add("张无忌");
		list.add("张三丰");
		list.add("张飞");
		list.add("维斯布鲁克");
		
		Stream<String> s = list.stream();
		System.out.println(s.count());
	}
	private static void test02(ArrayList<String> list) {
		Stream<String> str = list.stream();
		Function<String, Integer> fun = x -> x.length();
		str.map(fun).forEach(x ->System.out.println(x));
	}
	private static void test01(ArrayList<String> list) {
		Stream<String> str = list.stream();
		Predicate<String> pre = x -> x.startsWith("张");
		Consumer<String> con = x -> System.out.println(x);
		str.filter(pre).forEach(con);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值