-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamJava8Part1
More file actions
218 lines (180 loc) · 9.02 KB
/
StreamJava8Part1
File metadata and controls
218 lines (180 loc) · 9.02 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//Basic Stream Practice Examples
//Queries:https://javabypatel.blogspot.com/2018/06/java-8-stream-practice-problems.html
/*Operations to be done
Get student with exact match name "jayesh"
Get student with matching address "1235"
Get all student having mobile numbers 3333.
Get all student having mobile number 1233 and 1234
Create a List<Student> from the List<TempStudent>
Convert List<Student> to List<String> of student name
Convert List<students> to String
Change the case of List<String>
Sort List<String>
Conditionally apply Filter condition, say if flag is enabled then.
*/
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Comparator;
public class MyClass {
public static void main(String args[]) {
Student student1 = new Student(
"Jayesh",
22,
new Address("1234"),
Arrays.asList(new MobileNumber("1233"), new MobileNumber("3333")));
Student student2 = new Student(
"Khyati",
20,
new Address("1235"),
Arrays.asList(new MobileNumber("1111"), new MobileNumber("3333"), new MobileNumber("1233")));
Student student3 = new Student(
"Jason",
20,
new Address("1236"),
Arrays.asList(new MobileNumber("3333"), new MobileNumber("4444")));
List<Student> students = Arrays.asList(student1, student2, student3);
// Get student with exact match name "jayesh"
//---------------------------------------------------------------------------------------
Optional<Student> stud = students.stream()
.filter(student -> student.getName().equals("Jayesh"))
.findFirst();
System.out.println(stud.isPresent() ? stud.get().getName() : "No student found");
//----------------------------------------------------------------------------------------
// Sort student list in descending order based on student's name
//---------------------------------------------------------------------------------------------------------------------------------
List<Student> stud=students.stream().sorted(Comparator.comparing(Student::getName).reversed()).collect(Collectors.toList());
for(Student i:stud){System.out.println(i.toString());}
//---------------------------------------------------------------------------------------------------------------------------------
//Get student with matching address "1235"
//---------------------------------------------------------------------------------------------------------------------------------
Optional<Student> studAdd=students.stream().filter(i->i.getAddress().getZipcode()=="1235").findFirst();
System.out.println(studAdd.toString());
//---------------------------------------------------------------------------------------------------------------------------------
//Get all student having mobile numbers 3333, then sort based on age, if age is equal sort based on name
//---------------------------------------------------------------------------------------------------------------------------------
List<Student> studm=students.stream().filter(i->i.getMobileNumbers().stream().anyMatch(x->x.getNumber().equals("3333"))).sorted(Comparator.comparing(Student::getAge).thenComparing(Student::getName)).collect(Collectors.toList());
for(Student i:studm){System.out.println(i.toString());}
//---------------------------------------------------------------------------------------------------------------------------------
//Get all student having mobile number 1233 and 1234
//---------------------------------------------------------------------------------------------------------------------------------
List<Student> stud3 = students.stream()
.filter(student -> student.getMobileNumbers().stream().allMatch(x -> Objects.equals(x.getNumber(), "1233") || Objects.equals(x.getNumber(), "1234")))
.collect(Collectors.toList());
//---------------------------------------------------------------------------------------------------------------------------------
//Create a List<Student> from the List<TempStudent>
//---------------------------------------------------------------------------------------------------------------------------------
TempStudent tmpStud1 = new TempStudent(
"Jayesh1",
201,
new Address("12341"),
Arrays.asList(new MobileNumber("12331"), new MobileNumber("12341")));
TempStudent tmpStud2 = new TempStudent(
"Khyati1",
202,
new Address("12351"),
Arrays.asList(new MobileNumber("11111"), new MobileNumber("33331"), new MobileNumber("12331")));
List<TempStudent> tmpStudents = Arrays.asList(tmpStud1, tmpStud2);
List<Student> studList=tmpStudents.stream().map(t->new Student(t.name,t.age,t.address,t.mobileNumbers)).collect(Collectors.toList());
System.out.println();
for(Student i:studList){System.out.println(i.toString());}
//---------------------------------------------------------------------------------------------------------------------------------
//Convert List<Student> to List<String> of student name
//---------------------------------------------------------------------------------------------------------------------------------
List<String> nameList=studList.stream().map(Student::getName).collect(Collectors.toList());
for(String i:nameList){System.out.println(i);}
//---------------------------------------------------------------------------------------------------------------------------------
//Convert List<students> to String
//---------------------------------------------------------------------------------------------------------------------------------
String name=nameList.stream().map(i->i).collect(Collectors.joining(",","[","]"));
System.out.println(name);
//---------------------------------------------------------------------------------------------------------------------------------
//Change the case of List<String>, Sort List<String> in descending order
//---------------------------------------------------------------------------------------------------------------------------------
nameList.stream().map(String::toUpperCase).sorted(Comparator.reverseOrder()).forEach(System.out::println);
//---------------------------------------------------------------------------------------------------------------------------------
/*Here key imp concept, lambda doesn't work with boolean coz boolean is not a functional interface*/
/*You can use forEach as a terminal operator too*/
}
}
class TempStudent {
public String name;
public int age;
public Address address;
public List<MobileNumber> mobileNumbers;
public TempStudent(String name, int age, Address address, List<MobileNumber> mobileNumbers) {
this.name = name;
this.age = age;
this.address = address;
this.mobileNumbers = mobileNumbers;
}
}
class Student{
private String name;
private int age;
private Address address;
private List<MobileNumber> mobileNumbers;
public Student(String name, int age, Address address, List<MobileNumber> mobileNumbers) {
this.name = name;
this.age = age;
this.address = address;
this.mobileNumbers = mobileNumbers;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Address getAddress() {
return address;
}
public List<MobileNumber> getMobileNumbers() {
return mobileNumbers;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(Address address) {
this.address = address;
}
public void setMobileNumbers(List<MobileNumber> mobileNumbers) {
this.mobileNumbers = mobileNumbers;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
", mobileNumbers=" + mobileNumbers +
'}';
}
}
class Address{
private String zipcode;
public Address(String zipcode) {
this.zipcode = zipcode;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
class MobileNumber{
private String number;
public MobileNumber(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}