-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA0_StreamExamples.java
More file actions
122 lines (94 loc) · 4.83 KB
/
Copy pathA0_StreamExamples.java
File metadata and controls
122 lines (94 loc) · 4.83 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
package java8.a2_streams;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class A0_StreamExamples {
public static void main(String[] args) {
// 1. Remove Duplicates From List
List<String> list1 = Arrays.asList("java", "python", "java");
List<String> unique = list1.stream().distinct().collect(Collectors.toList());
System.out.println("1. Unique List: " + unique); // [java, python]
// 2. Sort List in Reverse Order
List<Integer> list2 = Arrays.asList(3, 1, 4, 2);
System.out.print("2. Reverse Sorted List: ");
list2.stream().sorted(Comparator.reverseOrder()).forEach(i -> System.out.print(i + " "));
System.out.println();
// 3. Join Strings with Delimiter, Prefix, Suffix
List<String> names = Arrays.asList("Rizwan", "Ranveer", "Vishaka");
String joined = names.stream().collect(Collectors.joining(", ", "Names: [", "]"));
System.out.println("3. Joined String: " + joined); // Names: [Rizwan, Ranveer, Vishaka]
// 4. Print Multiples of 5
List<Integer> list4 = Arrays.asList(5, 10, 13, 15, 18);
System.out.print("4. Multiples of 5: ");
list4.stream().filter(i -> i % 5 == 0).forEach(i -> System.out.print(i + " "));
System.out.println();
// 5. Maximum in List
List<Integer> list5 = Arrays.asList(10, 20, 30);
int max = list5.stream().max(Integer::compare).get();
System.out.println("5. Maximum: " + max); // 30
// 6. Minimum in List
List<Integer> list6 = Arrays.asList(10, 20, 30);
int min = list6.stream().min(Integer::compare).get();
System.out.println("6. Minimum: " + min); // 10
// 7. Merge Two Arrays Into Sorted List
int[] a = { 1, 3, 5 };
int[] b = { 2, 4, 6 };
int[] merged = IntStream.concat(Arrays.stream(a), Arrays.stream(b)).sorted().toArray();
System.out.println("7. Merged Sorted Array: " + Arrays.toString(merged)); // [1, 2, 3, 4, 5, 6]
// 8. Sum of even Numbers
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
int sum = numbers.stream().filter(n -> n % 2 == 0) // keep even numbers
.mapToInt(Integer::intValue) // map to int (primitive)
.sum(); // sum them
System.out.println("8. Sum of even numbers: " + sum);
// 9. Stream Operations on List of Names
List<String> memberNames = new ArrayList<>();
memberNames.add("Amitabh");
memberNames.add("Shekhar");
memberNames.add("Aman");
memberNames.add("Rahul");
memberNames.add("Shahrukh");
memberNames.add("Salman");
memberNames.add("Yana");
memberNames.add("Lokesh");
System.out.println("9. DataSet for next Operations :: " + memberNames);
// 9.1 Sorted and Uppercase Names
List<String> sortedUpper = memberNames.stream().sorted().map(String::toUpperCase).collect(Collectors.toList());
System.out.println("9.1 Sorted Uppercase Names: " + sortedUpper);
// 9.2 Names Starting with 'A' in Uppercase
List<String> aUpper = memberNames.stream().filter(s -> s.startsWith("A")).map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println("9.2 Names Starting with 'A' in Uppercase: " + aUpper);
// 9.3 Names Starting with 'A'
List<String> aNames = memberNames.stream().filter(s -> s.startsWith("A")).collect(Collectors.toList());
System.out.println("9.3 Names Starting with 'A': " + aNames);
// 9.4 Any name starts with 'A'?
boolean anyStartsWithA = memberNames.stream().anyMatch(s -> s.startsWith("A"));
System.out.println("9.6 Any name starts with 'A'?: " + anyStartsWithA);
// 9.5 All names start with 'A'?
boolean allStartWithA = memberNames.stream().allMatch(s -> s.startsWith("A"));
System.out.println("9.7 All names start with 'A'?: " + allStartWithA);
// 9.6 No name starts with 'A'?
boolean noneStartWithA = memberNames.stream().noneMatch(s -> s.startsWith("A"));
System.out.println("9.8 No name starts with 'A'?: " + noneStartWithA);
// 9.7 Count of names starting with 'A'
long countA = memberNames.stream().filter(s -> s.startsWith("A")).count();
System.out.println("9.9 Count of names starting with 'A': " + countA);
// 9.8 Reduce to single string
Optional<String> reduced = memberNames.stream().reduce((s1, s2) -> s1 + "#" + s2);
System.out.println("9.10 Reduced String: " + reduced.orElse(""));
// 9.9 First name starting with 'L'
Optional<String> firstL = memberNames.stream().filter(s -> s.startsWith("L")).findFirst();
System.out.println("9.11 First name starting with 'L': " + firstL.orElse("Not Found"));
// 10. Stream Reduce Examples
List<String> words = Arrays.asList("apple", "banana", "orange");
String concatWithColon = words.stream().reduce("", (acc, word) -> acc.isEmpty() ? word : acc + ":" + word);
System.out.println("10.1 concatWithColon : " + concatWithColon);
String concat = words.stream().reduce("", String::concat);
System.out.println("10.2 String concat : " + concat);
}
}