Java Stream API : Interview Programs - Part 7

Java Stream API : Interview Programs  - Part 7

Java Stream API : Interview Programs  - Part 7
Java Stream API : Interview Programs  - Part 7

Write a program to Compute the cumulative sum of elements in a list using Java Stream API

You can use the Java Stream API to compute the cumulative sum of elements in a list. Here's a simple program to demonstrate this:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

List<Integer> cumulativeSum = numbers.stream()
.mapToInt(Integer::intValue)
.boxed()
.collect(Collectors.toList());

for (int i = 1; i < cumulativeSum.size(); i++) {
cumulativeSum.set(i, cumulativeSum.get(i) + cumulativeSum.get(i - 1));
}

System.out.println("Original List: " + numbers);
System.out.println("Cumulative Sum: " + cumulativeSum);
}
}

This program first converts the List<Integer> to an IntStream, then boxes it back to Integer objects. It then collects the elements into a list. Finally, it iterates over the list to compute the cumulative sum.


advertisement

Write a program to Find the Kth smallest element in a list using Java Stream API


You can find the Kth smallest element in a list using the Java Stream API by sorting the list and then selecting the Kth element. Here's a program to demonstrate this:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class KthSmallestElement {

public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(10, 2, 6, 8, 4, 12, 7);
int k = 3; // Find the 3rd smallest element

int kthSmallest = findKthSmallest(numbers, k);
System.out.println("The " + k + "th smallest element is: " + kthSmallest);
}

public static int findKthSmallest(List<Integer> numbers, int k) {
List<Integer> sortedList = numbers.stream()
.sorted()
.collect(Collectors.toList());

return sortedList.get(k - 1);
}
}

In this program, we first create a list of integers numbers. Then, we use the stream() method to convert the list into a stream. We sort the stream using the sorted() method and collect the sorted stream back into a list. Finally, we return the Kth smallest element from the sorted list using get(k - 1) since the index is zero-based.


advertisement

Write a program to Combine multiple maps into a single map using Java Stream API


You can combine multiple maps into a single map using the Java Stream API. Here's a simple example that demonstrates this:

import java.util.*;
import java.util.stream.Collectors;

public class CombineMapsExample {
public static void main(String[] args) {
// Creating multiple maps
Map<String, Integer> map1 = new HashMap<>();
map1.put("A", 1);
map1.put("B", 2);

Map<String, Integer> map2 = new HashMap<>();
map2.put("C", 3);
map2.put("D", 4);

Map<String, Integer> map3 = new HashMap<>();
map3.put("E", 5);
map3.put("F", 6);

// Combining maps using Java Stream API
Map<String, Integer> combinedMap = Stream.of(map1, map2, map3)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

// Printing the combined map
System.out.println(combinedMap);
}
}

This program creates three maps (map1, map2, and map3) and then combines them into a single map (combinedMap) using the flatMap operation and Collectors.toMap method of the Stream API.


advertisement

Write a program to Replace null values in a list with default values using Java Stream API

You can use the Java Stream API to replace null values in a list with default values. Here's an example program that demonstrates this:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ReplaceNullValues {
public static void main(String[] args) {
List<String> list = Arrays.asList("a", null, "b", null, "c", null);

List<String> replacedList = list.stream()
.map(value -> value != null ? value : "default")
.collect(Collectors.toList());

System.out.println("Original list: " + list);
System.out.println("List with nulls replaced: " + replacedList);
}
}

This program creates a list with some null values and then uses the map function to replace each null value with "default". Finally, it collects the elements into a new list. The output will be:

Original list: [a, null, b, null, c, null]
List with nulls replaced: [a, default, b, default, c, default]


advertisement

Post a Comment

Previous Post Next Post