好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java中lambda(函数式编程)一行解决foreach循环问题

java lambda(函数式编程)一行解决foreach循环

首先给大家推荐《精通lambda表达式:java多核编程》

这本书详细介绍了lambda表达式从入门到理解、应用

下面介绍用以前的循环方式进行对比,来更加清晰地java函数式编程中foreach的用法

一、以前我们使用的for循环

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/**

      * for循环

      */

     @Test

     public void forTest() {

         // 实例化一个List

         List<Point> points = Arrays.asList( new Point( 1 , 2 ), new Point( 2 , 3 ));

         System.out.println( "循环操作前:" + points);

         // for循环

         for ( int i = 0 ; i < points.size(); i++) {

             points.get(i).translate( 1 , 1 );

         }

         System.out.println( "循环操作后:" + points);

     }

二、后来使用foreach循环(感觉爽多了)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

/**

      * foreach循环

      */

     @Test

     public void forEachTest() {

         List<Point> points = Arrays.asList( new Point( 1 , 2 ), new Point( 2 , 3 ));

         System.out.println( "循环操作前:" + points);

         // foreach 循环

         for (Point p : points) {

             p.translate( 1 , 1 );

         }

         System.out.println( "循环操作后:" + points);

     }

三、而大家可能很少了解ArrayList中的forEach方法

下面便是一个实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/**

      * 调用ArrayList中foreach方法循环

      */

     @Test

     public void forEachMethodTest() {

         List<Point> points = Arrays.asList( new Point( 1 , 2 ), new Point( 2 , 3 ));

         System.out.println( "循环操作前:" + points);

         /**

          * 此foreach为ArrayList中的方法

          * 接收的参数为Consumer接口

          */

         points.forEach( new Consumer<Point>() {

             public void accept(Point t) {

                 t.translate( 1 , 1 );

             }

         });

         System.out.println( "循环操作后:" + points);

     }

ArrayList中的forEach方法源码实现

?

1

2

3

4

5

6

7

8

9

10

11

12

/**

      * ArrayList中的foreach方法

      * this为当前集合对象

      * Consumer接口里面的accept方法是对集合中对象所做的操作

      * 底层依然是foreach循环实现的

      */

     default void forEach(Consumer<? super T> action) {

         Objects.requireNonNull(action);

         for (T t : this ) {

             action.accept(t);

         }

     }

四、最后,逼格最高,写法最简洁,用起来最爽的函数式写法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/**

      * lambdaForeach循环

      */

     @Test

     public void lambdaForEachTest() {

         List<Point> points = Arrays.asList( new Point( 1 , 2 ), new Point( 2 , 3 ));

         System.out.println( "循环操作前:" + 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( "循环操作后:" + points);

     }

四种方式的执行结果:

循环操作前:[java.awt.Point[x=1,y=2], java.awt.Point[x=2,y=3]]

循环操作后:[java.awt.Point[x=2,y=3], java.awt.Point[x=3,y=4]]

java Lambda 与forEach

lambda表达式

λ表达式本质上是一个匿名方法。

?

1

2

3

public int add( int x, int y) {

     return x + y;

}

转成λ表达式后是这个样子:

?

1

(x, y) -> x + y; //返回两数之和

1、普通方式遍历 Map

?

1

2

3

4

5

6

7

8

9

10

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 表达式循环 Map

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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" );

     }

});

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/Icannotdebug/article/details/78595437

查看更多关于java中lambda(函数式编程)一行解决foreach循环问题的详细内容...

  阅读:15次