Skip to content

Commit 5b2dc13

Browse files
authored
Create App.java
1 parent e9455a5 commit 5b2dc13

File tree

1 file changed

+276
-0
lines changed

1 file changed

+276
-0
lines changed
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
package java_stream_api;
2+
3+
public class App {
4+
// 1. Find the number of elements in a list of strings.
5+
public static long findNumberOfElements(List<String> strList) {
6+
return strList.stream().count();
7+
}
8+
9+
// 2. Convert all strings in a list to uppercase.
10+
public static List<String> convertStringsToUppercase(List<String> strList) {
11+
return strList.stream().map(String::toUpperCase).collect(Collectors.toList());
12+
}
13+
14+
// 3. Filter a list of strings, keeping only those that start with the letter 'A'.
15+
public static List<String> filterStringsStartingWithA(List<String> strList) {
16+
return strList.stream().filter(el -> el.startsWith("A")).collect(Collectors.toList());
17+
}
18+
19+
// 4. Obtain unique integers from a list.
20+
public static List<Integer> uniqueIntegers(List<Integer> intsList) {
21+
return intsList.stream().distinct().collect(Collectors.toList());
22+
}
23+
24+
// 5. Calculate the sum of all numbers in a list of integers.
25+
public static int sumOfIntegers(List<Integer> intsList) {
26+
return intsList.stream().mapToInt(Integer::intValue).sum();
27+
}
28+
29+
// 6. Find the minimum number in a list of integers.
30+
public static int minOfIntegers(List<Integer> intsList) {
31+
return intsList.stream().mapToInt(Integer::intValue).min().orElse(Integer.MIN_VALUE);
32+
}
33+
34+
// 7. Find the maximum number in a list of integers.
35+
public static int maxOfIntegers(List<Integer> intsList) {
36+
return intsList.stream().mapToInt(Integer::intValue).max().orElse(Integer.MAX_VALUE);
37+
}
38+
39+
// 8. Concatenate all strings from a list into one string, separated by commas.
40+
public static String concatenateStringsWithComma(List<String> strList) {
41+
return strList.stream().collect(Collectors.joining(", "));
42+
}
43+
44+
// 9. Get the first element of a list of integers or 0 if the list is empty.
45+
public static int getFirstIntegerOrZero(List<Integer> intsList) {
46+
return intsList.stream().mapToInt(Integer::intValue).findFirst().orElse(0);
47+
}
48+
49+
// 10. Get the last element of a list of strings or "" if empty.
50+
public static String getLastString(List<String> strList) {
51+
return strList.stream().reduce((first, second) -> second).orElse("");
52+
}
53+
54+
// 11. Convert a list of integers into a list of their squares.
55+
public static List<Integer> listOfSquares(List<Integer> intsList) {
56+
return intsList.stream().map(i -> i * i).collect(Collectors.toList());
57+
}
58+
59+
// 12. Filter a list of integers, keeping only even numbers.
60+
public static List<Integer> filterEvenNumbers(List<Integer> intsList) {
61+
return intsList.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());
62+
}
63+
64+
// 13. Find people older than 18 years and return a list of their names.
65+
public static List<String> namesOfUsersOlderThan18(List<User> users) {
66+
return users.stream().filter(u -> u.getAge() > 18).map(User::getName).collect(Collectors.toList());
67+
}
68+
69+
// 14. Sort a list of strings by their length.
70+
public static List<String> sortStringsByLength(List<String> strList) {
71+
return strList.stream().sorted(Comparator.comparing(String::length)).collect(Collectors.toList());
72+
}
73+
74+
// 15. Check if a list of strings contains at least one string that includes the word "Java."
75+
public static boolean containsStringWithJava(List<String> strList) {
76+
return strList.stream().anyMatch(str -> str.contains("Java"));
77+
}
78+
79+
// 16. Merge two lists of strings into one.
80+
public static List<String> mergeTwoStringLists(List<String> list1, List<String> list2) {
81+
return Stream.concat(list1.stream(), list2.stream()).collect(Collectors.toList());
82+
}
83+
84+
// 17. Get the average value of all numbers in a list of integers.
85+
public static double averageOfIntegers(List<Integer> intsList) {
86+
return intsList.stream().mapToInt(Integer::intValue).average().orElse(0.0);
87+
}
88+
89+
// 18. Discard the first three elements in a list of integers.
90+
public static List<Integer> discardFirstThreeIntegers(List<Integer> intsList) {
91+
return intsList.stream().skip(3).collect(Collectors.toList());
92+
}
93+
94+
// 19. Keep only the first three elements of a list of integers.
95+
public static List<Integer> keepFirstThreeIntegers(List<Integer> intsList) {
96+
return intsList.stream().limit(3).collect(Collectors.toList());
97+
}
98+
99+
// 20. Convert each string into a list of integers representing the character codes.
100+
public static List<Integer> stringToAsciiCodes(List<String> strList) {
101+
return strList.stream().flatMapToInt(String::chars).boxed().collect(Collectors.toList());
102+
}
103+
104+
// 21. Count the number of strings longer than 5 characters.
105+
public static long countStringsLongerThan5(List<String> strList) {
106+
return strList.stream().filter(s -> s.length() > 5).count();
107+
}
108+
109+
// 22. Create a map from a list of strings where key - string, value - its length.
110+
public static Map<String, Integer> stringsToLengthMap(List<String> strList) {
111+
return strList.stream().collect(Collectors.toMap(Function.identity(), String::length));
112+
}
113+
114+
// 23. Sort a list of users by their age.
115+
public static List<User> sortUsersByAge(List<User> users) {
116+
return users.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
117+
}
118+
119+
// 24. Find the user with the maximum age.
120+
public static Optional<User> userWithMaxAge(List<User> users) {
121+
return users.stream().max(Comparator.comparing(User::getAge));
122+
}
123+
124+
// 25. Check if all strings in a list are longer than 3 characters.
125+
public static boolean areAllStringsLongerThan3(List<String> strList) {
126+
return strList.stream().allMatch(s -> s.length() > 3);
127+
}
128+
129+
// 26. Select all every 3rd element from a list of strings (1-based).
130+
public static List<String> selectEvery3rd(List<String> strList) {
131+
AtomicInteger counter = new AtomicInteger();
132+
return strList.stream().filter(x -> counter.incrementAndGet() % 3 == 0).collect(Collectors.toList());
133+
}
134+
135+
// 27. Calculate the total number of hobbies all users have.
136+
public static int totalHobbiesCount(List<User> users) {
137+
return users.stream().mapToInt(u -> u.getHobbies().size()).sum();
138+
}
139+
140+
// 28. Find a list of strings that appear more than once in a list.
141+
public static List<String> duplicateStrings(List<String> strList) {
142+
return strList.stream()
143+
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
144+
.entrySet().stream().filter(e -> e.getValue() > 1).map(Map.Entry::getKey)
145+
.collect(Collectors.toList());
146+
}
147+
148+
// 29. Convert all users' birthdates to string "AGE MM YYYY".
149+
public static List<User> convertBirthdatesToString(List<User> users) {
150+
users.forEach(user -> user.setBirthdate(user.getAge() + " MM YYYY"));
151+
return users;
152+
}
153+
154+
// 30. Filter a list of Person objects, keeping only those who live in "City X".
155+
public static List<Person> filterPersonsByCity(List<Person> persons, String city) {
156+
return persons.stream().filter(p -> "City X".equals(p.getCity())).collect(Collectors.toList());
157+
}
158+
159+
// 31. Reverse the order of elements in a list.
160+
public static <T> List<T> reverseList(List<T> list) {
161+
return IntStream.range(0, list.size())
162+
.mapToObj(i -> list.get(list.size() - 1 - i))
163+
.collect(Collectors.toList());
164+
}
165+
166+
// 32. Find the percentage of elements with the value "null" in a list.
167+
public static long percentageOfNulls(List<?> list) {
168+
long nullCount = list.stream().filter(Objects::isNull).count();
169+
return list.size() == 0 ? 0 : (nullCount * 100) / list.size();
170+
}
171+
172+
// 33. Shuffle the elements of a list randomly.
173+
public static <T> void shuffleList(List<T> list) {
174+
Collections.shuffle(list);
175+
}
176+
177+
// 34. Group numbers by their remainder when divided by 3.
178+
public static Map<Integer, List<Integer>> groupByRemainder3(List<Integer> intsList) {
179+
return intsList.stream().collect(Collectors.groupingBy(n -> n % 3));
180+
}
181+
182+
// 35. Convert a list of integers into a list of booleans: true for even, false for odd.
183+
public static List<Boolean> evenIntegersAsBooleans(List<Integer> intsList) {
184+
return intsList.stream().map(n -> n % 2 == 0).collect(Collectors.toList());
185+
}
186+
187+
// 36. Filter a list, keeping only unique elements, and return the result in reverse order.
188+
public static <T> List<T> uniqueElementsReversed(List<T> list) {
189+
List<T> unique = list.stream().distinct().collect(Collectors.toList());
190+
Collections.reverse(unique);
191+
return unique;
192+
}
193+
194+
// 37. Create a list of 100 random numbers and find the top 5 largest.
195+
public static List<Integer> top5Of100Random() {
196+
Random rnd = new Random();
197+
return rnd.ints(100, 0, 1000).boxed()
198+
.sorted(Comparator.reverseOrder()).limit(5)
199+
.collect(Collectors.toList());
200+
}
201+
202+
// 38. Split a list of strings into groups by their length.
203+
public static Map<Integer, List<String>> groupStringsByLength(List<String> strList) {
204+
return strList.stream().collect(Collectors.groupingBy(String::length));
205+
}
206+
207+
// 39. Find the element in a list that has the largest length.
208+
public static String longestString(List<String> strList) {
209+
return strList.stream().max(Comparator.comparing(String::length)).orElse("");
210+
}
211+
212+
// 40. Remove from the list all users whose name starts with "J."
213+
public static List<String> removeUsersWithJ(List<String> names) {
214+
return names.stream().filter(n -> !n.startsWith("J")).collect(Collectors.toList());
215+
}
216+
217+
// 41. Convert a list of integers into a map: number -> square.
218+
public static Map<Integer, Integer> integersToSquareMap(List<Integer> intsList) {
219+
return intsList.stream().collect(Collectors.toMap(n -> n, n -> n * n, (a, b) -> b));
220+
}
221+
222+
// 42. Find the total number of all words in a list of sentences.
223+
public static long totalWordsInSentences(List<String> sentences) {
224+
return sentences.stream().flatMap(s -> Arrays.stream(s.split("\\s+"))).count();
225+
}
226+
227+
// 43. Create a list of strings "yes" or "no," based on whether a number is even.
228+
public static List<String> yesNoIfEven(List<Integer> intsList) {
229+
return intsList.stream().map(n -> n % 2 == 0 ? "yes" : "no").collect(Collectors.toList());
230+
}
231+
232+
// 44. Calculate the range in a list of numbers.
233+
public static int rangeOfNumbers(List<Integer> intsList) {
234+
int min = intsList.stream().mapToInt(i -> i).min().orElse(0);
235+
int max = intsList.stream().mapToInt(i -> i).max().orElse(0);
236+
return max - min;
237+
}
238+
239+
// 45. List of lengths of strings, sorted in descending order.
240+
public static List<Integer> lengthsDescending(List<String> strList) {
241+
return strList.stream().map(String::length)
242+
.sorted(Comparator.reverseOrder()).collect(Collectors.toList());
243+
}
244+
245+
// 46. Find the sum of all string lengths in a list of strings.
246+
public static int sumStringLengths(List<String> strList) {
247+
return strList.stream().mapToInt(String::length).sum();
248+
}
249+
250+
// 47. Count the number of elements equal to null in a list.
251+
public static long countNullElements(List<?> list) {
252+
return list.stream().filter(Objects::isNull).count();
253+
}
254+
255+
// 48. Group users by their year of birth.
256+
public static Map<Integer, List<User>> groupUsersByBirthYear(List<User> users) {
257+
return users.stream().collect(Collectors.groupingBy(User::getBirthYear));
258+
}
259+
260+
// 49. Find all unique characters in a list of strings and form them into a sorted list.
261+
public static List<Character> uniqueSortedCharacters(List<String> strList) {
262+
return strList.stream()
263+
.flatMapToInt(String::chars)
264+
.mapToObj(c -> (char) c)
265+
.distinct()
266+
.sorted()
267+
.collect(Collectors.toList());
268+
}
269+
270+
// 50. Returns a list of user names, sorted by the length of these names.
271+
public static List<String> userNamesSortedByLength(List<User> users) {
272+
return users.stream().map(User::getName)
273+
.sorted(Comparator.comparingInt(String::length))
274+
.collect(Collectors.toList());
275+
}
276+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy