Skip to content

Latest commit

 

History

History
92 lines (77 loc) · 2.07 KB

foreach.md

File metadata and controls

92 lines (77 loc) · 2.07 KB

java8 foreach

在这篇文章中我将向你演示如何使用Java8中的foreach操作ListMap

1. Foreach操作Map

1.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());
}

1.2 使用Java8的foreach+lambda表达式遍历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");
	}
});

###2. Foreach操作List 2.1 普通方式循环List

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+lambda表达式遍历List

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

//Steam and filter
//Output : B
items.stream()
	.filter(s->s.contains("B"))
	.forEach(System.out::println);

参考资料:

  1. Java 8 Iterable forEach JavaDoc
  2. Java 8 forEach JavaDoc

欢迎star开源web框架Blade:http://github.com/biezhi/blade