Java Stream API : Interview Programs - Part 5

Java Stream API : Interview Programs  - Part 5

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

Write a program to Calculate prime factors of each element in a list using Java Stream API

You can use the Java Stream API to calculate the prime factors of each element in a list. Here's a program that demonstrates this:

import java.util.ArrayList;
import java.util.List;

public class PrimeFactorsCalculator {

public static void main(String[] args) {
List<Integer> numbers = List.of(12, 15, 20, 30, 35);

numbers.stream()
.forEach(number -> {
System.out.print("Prime factors of " + number + ": ");
calculatePrimeFactors(number).forEach(factor -> System.out.print(factor + " "));
System.out.println();
});
}

private static List<Integer> calculatePrimeFactors(int number) {
List<Integer> primeFactors = new ArrayList<>();
int divisor = 2;

while (number > 1) {
while (number % divisor == 0) {
primeFactors.add(divisor);
number /= divisor;
}
divisor++;
}

return primeFactors;
}
}


This program defines a method calculatePrimeFactors that calculates the prime factors of a given number. It then uses the Java Stream API to iterate over a list of numbers, calling calculatePrimeFactors for each number and printing the results. 


advertisement

Write a program to Find the intersection of multiple lists using Java Stream API.

You can find the intersection of multiple lists using the Java Stream API by converting the lists to streams and then using the `reduce` method to find the common elements. Here's a program that demonstrates this:

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

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

List<Integer> intersection = lists.stream()
.reduce((list1, list2) -> list1.stream()
.filter(list2::contains)
.collect(Collectors.toList()))
.orElse(new ArrayList<>());

System.out.println("Intersection: " + intersection);
}
}

This program creates three lists of integers and finds their intersection using the `reduce` method. The `filter` operation is used to check if each element in the first list is contained in the second list, and the `collect` method collects the common elements into a new list.


advertisement

Write a program to Combine multiple streams into a single stream using Java Stream API.

You can use the `Stream.concat` method to combine multiple streams into a single stream. Here's an example program that demonstrates this:

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

public class Main {
public static void main(String[] args) {
// Create multiple streams
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(4, 5, 6);
List<Integer> list3 = Arrays.asList(7, 8, 9);

// Combine streams
Stream<Integer> combinedStream = Stream.concat(
Stream.concat(list1.stream(), list2.stream()),
list3.stream()
);

// Collect and print elements from the combined stream
List<Integer> combinedList = combinedStream.collect(Collectors.toList());
System.out.println(combinedList);
}
}

In this example, we have three lists (`list1`, `list2`, and `list3`), and we combine them into a single stream using the `Stream.concat` method. Finally, we collect the elements of the combined stream into a list and print them.


advertisement

Write a program to Remove the nth element from a list using Java Stream API.

To remove the nth element from a list using the Java Stream API, you can convert the list to a stream, filter out the nth element, and then collect the stream back to a list. Here's an example:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class RemoveNthElement {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);

int n = 2; // Remove the 2nd element (index 1)

List<Integer> updatedList = IntStream.range(0, numbers.size())
.filter(i -> i != n)
.mapToObj(numbers::get)
.collect(Collectors.toList());

System.out.println("Original List: " + numbers);
System.out.println("Updated List: " + updatedList);
}
}

In this example, we have a list of integers `numbers`. We want to remove the element at index `n` (zero-based index). The `IntStream.range` method is used to create a stream of indices from 0 to `numbers.size() - 1`. We then use the `filter` method to exclude the index `n`, and `mapToObj` to get the elements corresponding to the indices. Finally, we collect the stream back to a list to get the updated list without the nth element.


advertisement

Post a Comment

Previous Post Next Post