好得很程序员自学网

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

Java中forEach使用lambda表达式,数组和集合的区别说明

1.数组怎么使用lambda

数组不能直接在forEach中使用lambda表达式

?

1

2

3

PartnerType[] values = PartnerType.values();

//提示Cannot resolve method 'forEach(<method reference>)

values.forEach(System.out::println); //错误使用

想要使用必须转换,如下

(1)转成list

(2)转成steam

?

1

2

3

PartnerType[] values = PartnerType.values();

Arrays.stream(values).forEach(System.out::println); //转成流

Arrays.asList(values).forEach(System.out::println); //转成list

2.集合怎么使用lambda

(1)list在forEach中使用lambda

?

1

2

3

4

5

ArrayList<String> arrayList = new ArrayList<>();

arrayList.add( "a" );

arrayList.add( "b" );

arrayList.add( "c" );

arrayList.forEach(System.out::println);

(2)map在forEach中使用lambda

?

1

2

3

4

5

6

HashMap<String, Integer> hashMap = new HashMap<>();

hashMap.put( "a" , 1 );

hashMap.put( "b" , 2 );

hashMap.put( "c" , 3 );

hashMap.put( "d" , 4 );

hashMap.forEach((k,v)->System.out.println(k+ "_" +v.intValue()));

Java8 forEach+Lambda表达式

1. forEach and Map

1.1 通常这样遍历一个Map

?

1

2

3

4

5

6

7

8

9

10

11

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

}

1.2 在java8中你可以使用 foreach + 拉姆达表达式遍历

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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

     }

});

2. forEach and List

2.1通常这样遍历一个List.

?

1

2

3

4

5

6

7

8

9

10

List<String> items = new ArrayList<>();

items.add( "A" );

items.add( "B" );

items.add( "C" );

items.add( "D" );

items.add( "E" );

 

for (String item : items){

     System.out.println(item);

}

2.2在java8中你可以使用 foreach + 拉姆达表达式 或者 method reference(方法引用)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

List<String> items = new ArrayList<>();

items.add( "A" );

items.add( "B" );

items.add( "C" );

items.add( "D" );

items.add( "E" );

 

//lambda

//Output : A,B,C,D,E

items.forEach(item->System.out.println(item));

 

//Output : C

items.forEach(item->{

     if ( "C" .equals(item)){

         System.out.println(item);

     }

});

 

//method reference

//Output : A,B,C,D,E

items.forEach(System.out::println);

 

//Stream and filter

//Output : B

items.stream()

     .filter(s->s.contains( "B" ))

     .forEach(System.out::println);

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

原文链接:https://blog.csdn.net/u010938610/article/details/82699321

查看更多关于Java中forEach使用lambda表达式,数组和集合的区别说明的详细内容...

  阅读:19次