Java Stream API : Interview Programs - Part 2

Java Stream API : Interview Programs - Part 2

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

Write a program to Filter palindromic strings from a list using Java Stream API

Here's a Java program that uses the Stream API to filter palindromic strings from a list:

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

public class Main {
public static void main(String[] args) {
List<String> words = Arrays.asList("level", "hello", "radar", "world", "civic");

List<String> palindromes = words.stream()
.filter(word -> isPalindrome(word))
.collect(Collectors.toList());

System.out.println("Palindromic words: " + palindromes);
}

private static boolean isPalindrome(String word) {
int length = word.length();
for (int i = 0; i < length / 2; i++) {
if (word.charAt(i) != word.charAt(length - 1 - i)) {
return false;
}
}
return true;
}
}

This program defines a isPalindrome method that checks if a given word is a palindrome. It then uses the Stream API to filter palindromic words from the input list words and collects them into a new list palindromes, which is then printed to the console.


advertisement

Write a program to Find the longest string from a list of strings using Java Stream API

You can find the longest string from a list of strings using the Java Stream API as follows:

import java.util.Arrays;
import java.util.List;

public class Main {
public static void main(String[] args) {
List<String> strings = Arrays.asList("Java", "Python", "JavaScript", "C++", "Ruby");

String longest = strings.stream()
.max((s1, s2) -> s1.length() - s2.length())
.orElse(null);

System.out.println("Longest string: " + longest);
}
}

In this example, we use the stream() method to create a stream from the list of strings. Then, we use the max method along with a comparator to find the longest string based on their lengths. Finally, we use orElse to handle the case where the stream is empty (i.e., the list is empty) and return null in that case.


advertisement

Write a program to Generate a Fibonacci series using Java Stream API

You can use the Java Stream API to generate a Fibonacci series. Here's a program that does that:

import java.util.stream.Stream;

public class FibonacciSeries {
public static void main(String[] args) {
int n = 10; // Number of Fibonacci numbers to generate
System.out.println("Generating Fibonacci series up to " + n + " numbers:");
Stream.iterate(new long[]{0, 1}, f -> new long[]{f[1], f[0] + f[1]})
.limit(n)
.map(f -> f[0])
.forEach(System.out::println);
}
}

This program uses Stream.iterate to generate an infinite stream of Fibonacci numbers, starting with {0, 1}. The limit(n) method is then used to limit the stream to the first n elements. Finally, the map method is used to extract the first element of each pair (f[0]) and forEach is used to print each Fibonacci number.


advertisement


Write a program to Combine two lists into a single list, alternating elements from each list using Java Stream API.

You can achieve this by using the IntStream.range method along with the flatMap operation in the Java Stream API. Here's a program that combines two lists into a single list, alternating elements from each list:

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

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

List<Integer> combinedList = IntStream.range(0, Math.min(list1.size(), list2.size()))
.flatMap(i -> IntStream.of(list1.get(i), list2.get(i)))
.boxed()
.collect(Collectors.toList());

// Add remaining elements from longer list
if (list1.size() > list2.size()) {
combinedList.addAll(list1.subList(list2.size(), list1.size()));
} else if (list2.size() > list1.size()) {
combinedList.addAll(list2.subList(list1.size(), list2.size()));
}

System.out.println("Combined List: " + combinedList);
}
}

This program first combines the elements at corresponding indexes from both lists, and then adds any remaining elements from the longer list to the end of the combined list.

Post a Comment

Previous Post Next Post