-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava8.java
More file actions
378 lines (311 loc) · 14.1 KB
/
Copy pathJava8.java
File metadata and controls
378 lines (311 loc) · 14.1 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package objectsexample;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Nodes {
int id;
String tagName;
int count;
Nodes(int id, String tagName, int count) {
this.id = id;
this.tagName = tagName;
this.count = count;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTagName() {
return tagName;
}
public void setTagName(String tagName) {
this.tagName = tagName;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
class Employee {
private int id;
private String name;
private int age;
private long salary;
public Employee(int id, String name, int age, long salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
class Person1 {
int id;
String name;
Person1(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Java8 {
public static void main(String[] args) {
// input
int[] arr = new int[] { 9, 10, 15, 8, 49, 25, 98, 32, 10 };
List<Integer> list = Arrays.asList(11, 10, 15, 8, 49, 25, 98, 32, 10);
// 1. to filter even nos from list
/*
* list.stream().filter(i -> i%2 ==0).forEach(System.out::println);
* list.stream().forEach((k) -> {
* System.out.print(k + " ");
* });
*/
// Arrays.stream(arr).filter(i ->i %2 == 0).forEach(System.out::println);
// 2.Given a list of integers, find out all the numbers starting with 1 using
// Stream functions
/*
* list.stream().map(s-> s + " " ).filter(s -> s.startsWith("1")).forEach((k)
* ->{
* System.out.println(k + " ");
* });
*
* Arrays.stream(arr).mapToObj(s -> s + " ").filter(s ->
* s.startsWith("1")).forEach((k) ->{
* System.out.println(k + " ");
* });
*/
// 3 .How to find duplicate elements in a given integers list in java using
// Stream functions
/*
* Set<Integer> set = new HashSet<>();
* list.stream().filter(n ->!set.add(n)).forEach(System.out::println);
*
* Arrays.stream(arr).filter(n ->!set.add(n)).forEach(System.out::println);
*/
// 4 .Given the list of integers, find the first element of the list using
// Stream functions
/*
* list.stream().findFirst().ifPresent(System.out::println);
* Arrays.stream(arr).findFirst().ifPresent(System.out::println);
*/
// 5 .Given a list of integers, find the total number of elements present in the
// list using Stream functions
/*
* System.out.println("count of list is ::"+list.stream().count());
* System.out.println("count of array is ::"+Arrays.stream(arr).count());
*/
// 6.find the maximum value element present in it using Stream functions
/*
* System.out.println("Maximum ::"+list.stream().max(Integer::compare).get());
* System.out.println("Maximum ::"+Arrays.stream(arr).max().getAsInt());
*/
// 7. find the first non-repeated character in it using Stream functions
// input.chars().mapToObj().collect(,,).entrySet().filter().map().getFirst().get()
String input = "Java articles are Awesome";
Character ch = input.chars().mapToObj(s -> Character.toLowerCase(Character.valueOf((char) s)))
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
.entrySet()
.stream().filter(entry -> entry.getValue() == 1).map(entry -> entry.getKey()).findFirst().get();
System.out.println("first character non repeating::" + ch);
// 8.Given a String, find the first repeated character in it using Stream
// functions
/*
* Character ch1 = input.chars().mapToObj(s ->
* Character.toLowerCase(Character.valueOf((char)s))).collect(Collectors.
* groupingBy(Function.identity(),LinkedHashMap::new ,
* Collectors.counting())).entrySet()
* .stream().filter(entry -> entry.getValue() >1).map(entry ->
* entry.getKey()).findFirst().get();
* System.out.println("first character repeating::"+ch1);
*/
/*
* String input = "Java articles are Awesome";
* String[] i = input.split("");
* Map<String, Long> collect =
* Stream.of(i).collect(Collectors.groupingBy(Function.identity(),
* Collectors.counting()));
* System.out.println(collect);
*/
// 9. a list of integers, sort all the values present in it using Stream
// functions
/*
* list.stream().sorted().forEach(System.out::println);
* Arrays.stream(arr).sorted().forEach(System.out::println);
*/
// 10.Given a list of integers, sort all the values present in it in descending
// order using Stream functions
// list.stream().sorted(Collections.reverseOrder()).forEach(System.out::println);
// Arrays.stream(arr).boxed().sorted(Collections.reverseOrder()).forEach(System.out::println);
// 11.Given an integer array nums, return true if any value appears at least
// twice in the array,
// and return false if every element is distinct
List<Integer> numList = Arrays.stream(arr).boxed().collect(Collectors.toList());
Set<Integer> set = new HashSet<>(numList);
boolean res = false;
if (numList.size() == set.size()) {
res = false;
} else {
res = true;
}
System.out.println("value appearing twice::" + res);
// 12. How will you get the current date and time using Java 8 Date and Time API
System.out.println("Current Local Date: " + java.time.LocalDate.now());
// Used LocalDate API to get the date
System.out.println("Current Local Time: " + java.time.LocalTime.now());
// Used LocalTime API to get the time
System.out.println("Current Local Date and Time: " + java.time.LocalDateTime.now());
// Used LocalDateTime API to get both date and time
// 13. Write a Java 8 program to concatenate two Streams?
List<String> list1 = Arrays.asList("Java", "8");
List<String> list2 = Arrays.asList("explained", "through", "programs");
Stream<String> concatStream = Stream.concat(list1.stream(), list2.stream());
// Concatenated the list1 and list2 by converting them into Stream
concatStream.forEach(k -> {
System.out.print(k + " ");
});
// 14. Java 8 program to perform cube on list elements and filter numbers
// greater than 50
// list.stream().map(i-> i*i*i).filter(i
// ->i>50).collect(Collectors.toList()).forEach(System.out::println);
// Arrays.stream(arr).map(i-> i*i*i).filter(i
// ->i>50).boxed().collect(Collectors.toList()).forEach(System.out::println);
// 15. Write a Java 8 program to sort an array and then convert the sorted array
// into Stream
Arrays.parallelSort(arr);
Arrays.stream(arr).forEach(System.out::println);
// 16 . How to use map to convert object into Uppercase in Java 8
List<String> names = Arrays.asList(new String[] { "Aa", "Bb", "Cc", "Dd", "Aa" });
System.out.println(names.stream().map(String::toLowerCase).collect(Collectors.toList()));
// 17. How to convert a List of objects into a Map by considering duplicated
// keys and store them in sorted order?
Map<String, Long> map = names.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(map);
// 18. How to find only duplicate elements with its count from the String
// ArrayList in Java8
Map<String, Long> mapDuplicate = names.stream().filter(x -> Collections.frequency(names, x) > 1)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println("dup" + mapDuplicate);
// 19. How to check if list is empty in Java 8 using Optional, if not null
// iterate through the list and print the object?
Nodes node1 = new Nodes(1, "vee", 1);
Nodes node2 = new Nodes(2, "viv", 2);
List<Nodes> nodesList = new ArrayList<>();
nodesList.add(node1);
nodesList.add(node2);
Optional.ofNullable(nodesList).orElseGet(Collections::emptyList).stream().filter(Objects::nonNull)
.map(Nodes::getTagName).forEach(s -> System.out.println("tag names is:" + s));
// 20. Program to find the Maximum element in an array
System.out.println(list.stream().max(Integer::compare).get());
System.out.println(Arrays.stream(arr).max().getAsInt());
// 21. program to print the count of each character in a String?
String s = "string data to count each character";
Map<String, Long> countMap = Arrays.stream(s.split("")).map(String::toLowerCase)
.collect(Collectors.groupingBy(str -> str, LinkedHashMap::new, Collectors.counting()));
System.out.println(countMap);
// 21. sort employee by salary in ascending order
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(10, "Ramesh", 30, 400000));
employees.add(new Employee(20, "John", 29, 350000));
employees.add(new Employee(30, "Tom", 30, 450000));
employees.add(new Employee(40, "Pramod", 29, 500000));
employees.add(new Employee(50, "Pramodini", 31, 500000));
List<Employee> resultAsc = employees.stream().sorted(((o1, o2) -> (int) (o1.getSalary() - o2.getSalary())))
.collect(Collectors.toList());
System.out.println("Ascending order::" + resultAsc);
System.out.println(
employees.stream().sorted(Comparator.comparingLong(Employee::getSalary)).collect(Collectors.toList()));
List<Employee> resultDesc = employees.stream().sorted(((o1, o2) -> (int) (o2.getSalary() - o1.getSalary())))
.collect(Collectors.toList());
System.out.println("Descending order::" + resultDesc);
System.out.println(employees.stream().sorted(Comparator.comparingLong(Employee::getSalary).reversed())
.collect(Collectors.toList()));
// second Highest Salary
// System.out.println("Second Highest salary::");
// employees.stream().collect(Collectors.groupingBy(Employee::getSalary)).entrySet();
// 22. odd even sum
int[] arr1 = new int[] { 1, 2, 4, 6, 7 };
System.out.println(Arrays.stream(arr1).boxed().filter(v -> v % 2 == 0)
.collect(Collectors.summingInt(Integer::intValue)));
System.out.println(Arrays.stream(arr1).boxed()
.filter(v -> v % 2 == 0).mapToInt(Integer::intValue).sum());
// sum of odd and even 8 12
System.out.println(Arrays.stream(arr1).mapToObj(ob -> Integer.valueOf(ob))
.collect(Collectors.groupingBy(t -> t % 2 == 0 ? "ODD" : "EVEN")));
Map<String, Integer> map1 = Arrays.stream(arr1).boxed().collect(Collectors
.groupingBy(e -> e % 2 == 0 ? "OddSum" : "EvenSum", Collectors.summingInt(Integer::intValue)));
System.out.println("map::" + map1);
// 23. FindDuplicatesUsingStreamDistinctMethod
String[] companies = new String[] {
"Meta", "Apple", "Amazon", "Netflix", "Meta", "Google", "Apple" // duplicate
};
System.out.println("1. Original String[] Array with duplicates : \n");
Arrays.stream(companies).forEach(System.out::println);
// 24. convert list to map
List<Person1> listPerson = new ArrayList<>();
listPerson.add(new Person1(1, "vna"));
listPerson.add(new Person1(2, "vid"));
Map<Integer, Person1> mapPerson = listPerson.stream()
.collect(Collectors.toMap(Person1::getId, Function.identity()));
System.out.println(mapPerson);
// 25.Find word count of the words in string
// o/p this -1, is-2 ,word-2,count-1,for-1
String str = "this is word count for word is good";
Map<String, Long> mapStr = Arrays.stream(str.split(" "))
.filter(x -> Collections.frequency(Arrays.asList(str.split(" ")), x) >= 1)
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
System.out.println(mapStr);
// o/p is-2 ,word-2,this -1,count-1,for-1
Arrays.stream(str.split(" "))
.filter(x -> Collections.frequency(Arrays.asList(str.split(" ")), x) >= 1)
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
.entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue().reversed()
.thenComparing(Map.Entry.comparingByKey()))
.forEach(System.out::println);
}
}