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
	}

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