forked from Java-Techie-jt/java8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForEachDemo.java
More file actions
49 lines (35 loc) · 1.05 KB
/
ForEachDemo.java
File metadata and controls
49 lines (35 loc) · 1.05 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.javatechie.stream.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
public class ForEachDemo {
// filter----> conditional check
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Murrit");
list.add("john");
list.add("piter");
list.add("marek");
list.add("mac");
for (String s : list) {
if (s.startsWith("m")) {
System.out.println(s);
}
}
list.stream().filter(t -> t.startsWith("m")).forEach(t -> System.out.println(t));
Map<Integer, String> map = new HashMap<>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
map.put(4, "d");
/*
* map.forEach((key,value)->System.out.println(key+": "+value));*/
map.entrySet().stream().filter(k->k.getKey()%2==0).forEach(obj->System.out.println(obj));
/*
* Consumer<String> consumer=(t)->System.out.println(t); for(String s1:list) {
* consumer.accept(s1); }
*/
}
}