Java Stream API : Interview Programs - Part 3

Java Stream API : Interview Programs  - Part 3

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

Write a program to Merge and sort multiple lists into a single sorted list using Java Stream API

To merge and sort multiple lists into a single sorted list using Java Stream API, you can follow this approach:

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

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

List<Integer> mergedAndSorted = Stream.of(list1, list2, list3)
.flatMap(List::stream)
.sorted()
.collect(Collectors.toList());

System.out.println("Merged and Sorted List: " + mergedAndSorted);
}
}

This program first creates three lists (list1, list2, and list3) containing integer values. It then merges these lists into a single stream using flatMap(), sorts the stream using sorted(), and collects the sorted elements into a new list using Collectors.toList(). Finally, it prints the merged and sorted list.

Write a program to Split a comma-separated string into individual elements and filter out the empty strings using Java Stream API

You can achieve this using the Java Stream API by splitting the comma-separated string and then filtering out the empty strings. Here's an example program to demonstrate this:

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

public class Main {
public static void main(String[] args) {
String input = "apple,banana,,orange,,";
String[] elements = input.split(",");

Stream<String> stream = Arrays.stream(elements)
.filter(s -> !s.isEmpty());

stream.forEach(System.out::println);
}
}

This program splits the input string "apple,banana,,orange,," into individual elements using the split(",") method, resulting in an array elements that contains ["apple", "banana", "", "orange", ""]. Then, it creates a stream from the array using Arrays.stream(elements) and filters out the empty strings using the filter method. Finally, it prints each non-empty element using forEach(System.out::println).

Write a program to Sort a list of objects based on a custom comparator using Java Stream API

You can use the Java Stream API along with a custom comparator to sort a list of objects. Here's an example:

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

class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

public class Main {
public static void main(String[] args) {
List<Person> persons = Arrays.asList(
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 35)
);

Comparator<Person> ageComparator = Comparator.comparingInt(Person::getAge);

List<Person> sortedPersons = persons.stream()
.sorted(ageComparator)
.collect(Collectors.toList());

sortedPersons.forEach(System.out::println);
}
}

In this example, we have a Person class with name and age fields. We create a list of Person objects and then use the sorted method of the Stream class along with a custom comparator (ageComparator) to sort the list based on the persons' ages. Finally, we collect the sorted elements into a new list using Collectors.toList() and print the sorted list.


advertisement

Write a program to Filter anagrams from a list of strings using Java Stream API.

You can filter anagrams from a list of strings using the Java Stream API by grouping the strings by their sorted forms and then filtering out groups with only one element. Here's how you can do it:

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

public class AnagramFilter {
public static void main(String[] args) {
List<String> words = Arrays.asList("listen", "pot", "part", "opt", "trap", "silent", "top", "this", "hello", "hits");
Map<String, List<String>> anagramGroups = words.stream()
.collect(Collectors.groupingBy(str -> sortString(str)));

List<String> anagrams = anagramGroups.values().stream()
.filter(group -> group.size() > 1)
.flatMap(Collection::stream)
.collect(Collectors.toList());

System.out.println("Anagrams: " + anagrams);
}

private static String sortString(String str) {
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
return new String(charArray);
}
}

In this program, the sortString method is used to sort the characters of a string, which will be used as a key in the groupingBy collector to group anagrams together. The resulting anagrams list will contain all the strings that are anagrams of each other from the input list.

Post a Comment

Previous Post Next Post