Skip to content

Commit 9e6302d

Browse files
authored
Create StreamFilterOperations.java
1 parent bf787ed commit 9e6302d

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.code.Arrays;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class StreamFilterOperations {
8+
9+
public static void main(String[] args) {
10+
11+
List<Integer> li = new ArrayList<>();
12+
li.add(10);
13+
li.add(50);
14+
li.add(20);
15+
li.add(30);
16+
17+
List<Integer> lst = li.stream().filter(i -> i > 20).collect(Collectors.toList());
18+
19+
lst.forEach(x -> System.out.println(x));
20+
21+
System.out.println("------------------------------------------------------");
22+
//
23+
24+
List<PersonBean> person = new ArrayList<>();
25+
person.add(new PersonBean(1001,"tim",5685.10));
26+
person.add(new PersonBean(1002,"stine",6685.10));
27+
person.add(new PersonBean(1003,"adam",5675.10));
28+
person.add(new PersonBean(1004,"kingsly",6785.20));
29+
person.add(new PersonBean(1005,"torrento",8985.40));
30+
31+
person.stream().filter(p -> p.getId() == 1003).forEach(p -> System.out.println(p.getName()));
32+
33+
String pname = person.stream().filter(p -> p.getId() == 1003).map(s -> s.getName()).reduce("", String::concat);
34+
String pnamea = person.stream().filter(p -> p.getId() == 1003).map(s -> s.getName()).collect(Collectors.joining(""));
35+
System.out.println(pname);
36+
System.out.println(pnamea);
37+
String idname = person.stream().filter(p -> p.getSalary() == 8985.40).map(s -> s.getId()+","+s.getName()).collect(Collectors.joining(""));
38+
System.out.println(idname);
39+
40+
System.out.println("------------------------------------------------------");
41+
42+
PersonBean updperson = person.stream().filter(p -> p.getName().equals("stine")).findAny().orElse(null);
43+
System.out.println(updperson.toString());
44+
45+
System.out.println("------------------------------------------------------");
46+
47+
double sal = person.stream().filter(p -> { if(p.getName().equals("tim") && p.getId() == 1001)
48+
return true;
49+
return false;
50+
}).collect(Collectors.averagingDouble(v -> v.getSalary()));
51+
52+
System.out.println(sal);
53+
54+
double sala = person.stream().filter(p -> { if(p.getName().equals("tim") && p.getId() == 1001)
55+
return true;
56+
return false;
57+
}).map(p -> p.getSalary()).findAny().orElse(null);
58+
59+
System.out.println(sala);
60+
61+
System.out.println("------------------------------------------------------");
62+
63+
String all = li.stream().sorted().map(i -> String.valueOf(i)).collect(Collectors.joining(","));
64+
System.out.println(all);
65+
66+
}
67+
68+
69+
}

0 commit comments

Comments
 (0)