java8新特性——Lambda表达式

函数式接口

lambda表达式需要一个函数式接口的支持

函数式接口:只有一个抽象方法的接口,可用@FunctionalInterface注解修饰,检查该接口是否为函数式接口

如果再多写一个方法就会报错

@FunctionalInterface
interface Compare {
	Integer getValue(Integer i);
}

Lambda基础语法

语法格式一

	/**
	 * 语法格式一:无参数,无返回值
	 * () -> System.out.println("Hello world");
	 */
	@Test
	public void test() {

        //原来:
		Runnable run1 = new Runnable() {
			@Override
			public void run() {
				System.out.println("hello");
			}
		};
		run1.run();

        //lambda:
		System.out.println("---------------------------");

		Runnable run2 = () -> System.out.println("hello");
		run2.run();
	}

语法格式二

	/**
	 * 语法格式二:有一个参数,无返回值  
	 * (x)->System.out.println("Hello world");
	 *
	 * 若只有一个参数,那么小括号可以不写)
	 * x -> System.out.println("Hello world");
	 */
	@Test
	public void test01() {
		Consumer<String> com = (x) -> System.out.println(x);
		com.accept("45");
	}

语法格式三

	/**
	 * 语法格式三:两个以上的参数,有返回值,并且lambda体中有多条语句
	 *		(x,y) -> {
	 *		 	System.out.println("多条语句");
	 *		 	return **;
	 *		}
	 */
	@Test
	public void test02() {
		Comparator<Integer> com = (x, y) -> {
			System.out.println("比较数字");
			return x - y;
		};
	}

语法格式四

	/**
	 * 语法格式四:若lambda体中只有一条语句,大括号可以不写  return也可以不写
	 */
	@Test
	public void test03() {
		Comparator<Integer> com = (x, y) -> x - y;
	}

语法格式五

	/**
	 * 语法格式五:Lambda表达式的参数列表可以不写数据类型,这是由于JVM通过上下文推断出数据类型,即"类型推断"
	 */
	@Test
	public void test04(){
		Comparator<Integer> com = (Integer x, Integer y) -> {
			System.out.println("比较数字");
			return x - y;
		};
	}		

内置的四大核心函数式接口

		Consumer<T>	:	消费型接口:传进去一个参数,然后什么都没有返回
			void accept(T t);

		Supplier<T>	:	供给型接口:返回一些内容和对象
			T get();

		Function<T,R>	函数型接口:T:参数 R:返回值   对t处理完返回一个R对象
			R apply(T t);

		Predicate<T>	断言型接口:传进参数,返回一个boolean
			boolean TraderTest(T t);

消费型接口

	/**
	 * Consumer<T>	:	消费性接口
	 */

	public void happy(double money, Consumer con) {
		con.accept(money);
	}

	@Test
	public void test1() {
		happy(200, System.out::println);	//200.0
	}
	

供给型接口

	/**
	 * Supplier<T>  供给型接口
	 */
	@Test
	public void test2() {
		//产生10个随机数,并放到list集合中
		List<Integer> list = getNumList(10, () -> (int) (Math.random() * 10));
		list.forEach(System.out::println);
	}

	//需求:产生一些指定个数的整数,并放入集合中
	public List<Integer> getNumList(int num, Supplier<Integer> sup) {
		List<Integer> list = new ArrayList<>();

		for (int i = 0; i < num; i++) {
			Integer integer = sup.get();
			list.add(integer);

		}
		return list;
	}

函数型接口

	/**
	 * Function<T,R>	函数型接口
	 * <p>
	 * 用于处理字符串
	 */
	@Test
	public void test3() {
		String str = strHandler("ABcdEfG", s -> s.substring(1, 2));
		System.out.println(str);
        //结果:B
	}

	public String strHandler(String str, Function<String, String> fun) {
		return fun.apply(str);
	}

断言型接口

	/**
	 * Predicate<T>	断言型接口
	 */
	@Test
	public void test4() {

		List<String> list = Arrays.asList("Hello","San","yi","world");
		//将数组中字符串长度大于2的字符串加入到新的数组
		List<String> strfilter = strfilter(list, (x) -> x.length() > 2);
		strfilter.forEach(System.out::println);
        //Hello
	   //San
	   //world
        
	}

	//需求:将满足条件的字符串放入集合中
	public List<String> strfilter(List<String> list, Predicate<String> predicate) {
		List<String> strList = new ArrayList<>();
		for (String str : list) {
			if (predicate.test(str)) {
				strList.add(str);
			}
		}
		return strList;

	}

在这里插入图片描述

练习一

/**
	 * function:
	 * 创建函数式接口,新建类将字符转成大写
	 * 再将一个字符串的2 4 位置作为索引截取字符串
	 */
	@FunctionalInterface
	interface MyInter {
		String getvalue(String str);
	}

	//对传入的值进行操作,然后打印
	class TestMyInter {
		public String toUpper(String str, MyInter myInter) {
			return myInter.getvalue(str);

		}
	}
	
	@Test
	public void test02() {
		TestMyInter t = new TestMyInter();

		String str = t.toUpper("AbCdefg", (s)->s.toUpperCase());
		System.out.println(str);//ABCDEFG


		String st = t.toUpper("AbCdefg", x -> x.substring(2, 4));
		System.out.println(st); //Cd

	}

练习二

	/**
	 * function:声明一个带两个泛型的函数式接口,泛型为<T,R> T为参数 R为返回值
	 */
	@FunctionalInterface
	interface MyFun<T, R> {
		R getValue(T t1, T t2);
	}

	//对两个数进行操作,打印结果
	class TestMyFun {
		public void op(Long l1, Long l2, MyFun<Long, Long> mf) {
			System.out.println(mf.getValue(l1, l2));
		}
	}
	@Test
	public void test03() {
		TestMyFun testMyFun = new TestMyFun();
		testMyFun.op(10l, 20l, (x, y) -> x + y); // 30
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章