Java Stream API Practice Solutions
Java Stream API Practice Solutions
The Stream API can create a map associating each unique string with its length by using the 'collect()' method with 'Collectors.toMap()'. The 'toMap()' method requires a key mapper function that maps each element to its key (the string itself) and a value mapper function that maps each string to its length, such as 'String::length'. This generates a map where each string from the input list is associated with its length. For example, with a list of words like "apple", "banana", and "kiwi", a resulting map is {"apple"=5, "banana"=6, "kiwi"=4} .
Flattening a list of lists using the Stream API involves creating a stream of the nested lists and then using 'flatMap()' to convert each sublist into a stream of its elements. By doing so, 'flatMap()' maps each list to a stream and flattens these into a single stream containing all elements. Collect the transformed stream into a list using 'collect(Collectors.toList())'. For example, given a nested list [[1, 2], [3, 4]], the process results in a flattened list [1, 2, 3, 4].
The Stream API concatenates a collection of strings using the 'Collectors.joining()' method, which allows specifying a delimiter. This method melds the elements of the stream into a single string separated by the given delimiter. For example, joining a list of words such as "apple", "banana", and "kiwi" with a comma and space results in the string "apple, banana, kiwi" .
The Java Stream API allows counting occurrences of each character in a string by converting the string into a character stream and then using 'Collectors.groupingBy()' with 'Collectors.counting()'. This involves converting the string into an IntStream of its char values using 'chars()', transforming it to a stream of Character objects via 'mapToObj()', and then collecting the results into a map. Each character is grouped by its identity, and the 'counting()' collector counts the number of occurrences for each character .
The strategy to find the first non-repeating character using the Stream API includes converting the string to a stream of characters, collecting them into a LinkedHashMap with their counts through 'Collectors.groupingBy()', and maintaining insertion order by using 'LinkedHashMap::new'. Then, filter map entries with counts of one and use 'findFirst()' to retrieve the first such character. For the string "stream", this approach identifies 't' as the first non-repeating character .
To find the employee with the highest salary using the Stream API, create a stream from the list of employees using 'stream()'. Use the 'max()' terminal operation, passing a comparator that compares employees based on their salary using 'Comparator.comparingDouble(Employee::getSalary)'. This will produce an optional containing the employee with the highest salary. In the given example, from the employees John ($3000), Jane ($4000), and Doe ($3500), the employee with the highest salary is Jane .
The Stream API enables the removal of null values from a list by using 'filter()' with 'Objects::nonNull', filtering out all null elements. Convert the original list into a stream, apply the filter, and then collect the filtered elements back into a list using 'collect(Collectors.toList())'. For example, removing nulls from the list ["one", null, "two", null, "three"] results in the list ["one", "two", "three"].
Merging two lists with unique elements using the Stream API entails creating streams from each list and concatenating them using 'Stream.concat()'. To ensure uniqueness, apply 'distinct()' to the resultant stream to remove duplicate elements before collecting them into a list with 'collect(Collectors.toList())'. For instance, merging lists [1, 2, 3] and [3, 4, 5] leads to the unique list [1, 2, 3, 4, 5].
To group a list of strings by their length using the Java Stream API, you need to perform the following steps: First, call the 'stream()' method on the list of strings to convert it into a stream. Next, utilize the 'collect()' method with 'Collectors.groupingBy()' to group the elements of the stream according to a classifier function, which in this case is 'String::length'. This will result in a map with keys representing string lengths and values being lists of strings with that length .
The Stream API can be utilized to filter numbers greater than a given threshold and calculate their average by first creating a stream from a list of numbers with the 'stream()' method. Then, employ the 'filter()' method to retain elements that satisfy the condition of being greater than the threshold (e.g., '> 10'). Convert these filtered numbers to an IntStream using 'mapToInt()', allowing for collection of statistical data such as average using the 'average()' method. For example, filtering numbers greater than 10 from the list [5, 15, 25, 3] and finding the average results in 20.0 .