java中l(wèi)ambda(函數(shù)式編程)一行解決foreach循環(huán)問題
java lambda(函數(shù)式編程)一行解決foreach循環(huán)
首先給大家推薦《精通lambda表達式:java多核編程》
這本書詳細介紹了lambda表達式從入門到理解、應用
下面介紹用以前的循環(huán)方式進行對比,來更加清晰地java函數(shù)式編程中foreach的用法
一、以前我們使用的for循環(huán)
/** * for循環(huán) */ @Test public void forTest() { // 實例化一個List List<Point> points = Arrays.asList(new Point(1, 2), new Point(2, 3)); System.out.println("循環(huán)操作前:" + points); // for循環(huán) for (int i = 0; i < points.size(); i++) { points.get(i).translate(1, 1); } System.out.println("循環(huán)操作后:" + points); }
二、后來使用foreach循環(huán)(感覺爽多了)
/** * foreach循環(huán) */ @Test public void forEachTest() { List<Point> points = Arrays.asList(new Point(1, 2), new Point(2, 3)); System.out.println("循環(huán)操作前:" + points); // foreach 循環(huán) for (Point p : points) { p.translate(1, 1); } System.out.println("循環(huán)操作后:" + points); }
三、而大家可能很少了解ArrayList中的forEach方法
下面便是一個實例
/** * 調用ArrayList中foreach方法循環(huán) */ @Test public void forEachMethodTest() { List<Point> points = Arrays.asList(new Point(1, 2), new Point(2, 3)); System.out.println("循環(huán)操作前:" + points); /** * 此foreach為ArrayList中的方法 * 接收的參數(shù)為Consumer接口 */ points.forEach(new Consumer<Point>() { public void accept(Point t) { t.translate(1, 1); } }); System.out.println("循環(huán)操作后:" + points); }
ArrayList中的forEach方法源碼實現(xiàn)
/** * ArrayList中的foreach方法 * this為當前集合對象 * Consumer接口里面的accept方法是對集合中對象所做的操作 * 底層依然是foreach循環(huán)實現(xiàn)的 */ default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } }
四、最后,逼格最高,寫法最簡潔,用起來最爽的函數(shù)式寫法
/** * lambdaForeach循環(huán) */ @Test public void lambdaForEachTest() { List<Point> points = Arrays.asList(new Point(1, 2), new Point(2, 3)); System.out.println("循環(huán)操作前:" + points); /** * 僅此一行就夠了 * p -> p.translate(1, 1) * points集合的泛型為Point,里面存儲的是Point對象 * p即Point對象,箭頭后面的即對此對象所做的操作,當然這個p可以隨便命名 * 比如Long類型的number: num -> sb.append(num + ","); * 場景是將一個id集合,使用StringBuilder拼接成逗號分隔的字符串 */ points.forEach(p -> p.translate(1, 1)); System.out.println("循環(huán)操作后:" + points); }
四種方式的執(zhí)行結果:
循環(huán)操作前:[java.awt.Point[x=1,y=2], java.awt.Point[x=2,y=3]]
循環(huán)操作后:[java.awt.Point[x=2,y=3], java.awt.Point[x=3,y=4]]
java Lambda 與forEach
lambda表達式
λ表達式本質上是一個匿名方法。
public int add(int x, int y) { return x + y; }
轉成λ表達式后是這個樣子:
(x, y) -> x + y; //返回兩數(shù)之和
1、普通方式遍歷 Map
Map<String, Integer> items = new HashMap<>(); items.put("A", 10); items.put("B", 20); items.put("C", 30); items.put("D", 40); items.put("E", 50); items.put("F", 60); for (Map.Entry<String, Integer> entry : items.entrySet()) { System.out.println("Item : " + entry.getKey() + " Count : " + entry.getValue()); }
2、使用 forEach + lambda 表達式循環(huán) Map
Map<String, Integer> items = new HashMap<>(); items.put("A", 10); items.put("B", 20); items.put("C", 30); items.put("D", 40); items.put("E", 50); items.put("F", 60); items.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v)); items.forEach((k,v)->{ System.out.println("Item : " + k + " Count : " + v); if("E".equals(k)){ System.out.println("Hello E"); } });
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Springboot項目基于Devtools實現(xiàn)熱部署步驟詳解
這篇文章主要介紹了Springboot項目基于Devtools實現(xiàn)熱部署,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06詳解spring boot starter redis配置文件
spring-boot-starter-Redis主要是通過配置RedisConnectionFactory中的相關參數(shù)去實現(xiàn)連接redis service。下面通過本文給大家介紹在spring boot的配置文件中redis的基本配置,需要的的朋友參考下2017-07-07在idea中創(chuàng)建SpringBoot模塊的兩種方式
這篇文章主要介紹了在idea中創(chuàng)建一個SpringBoot模塊,本文給大家分享兩種方式,每種方式分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05