Profile Photo

Part-II : Stream API Coding Questions

Created on: Sep 18, 2024

Stream api

  1. Partition a array into two parts based on even and odd.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); Map<Boolean, List<Integer>> evenOddPartition = numbers.stream() .collect(Collectors.partitioningBy(n->n%2==0)); System.out.println(evenOddPartition);
{false=[1, 3, 5, 7, 9], true=[2, 4, 6, 8]}
  1. Find the longest string in a list of strings using Java streams ?
List<String> strings = Arrays .asList("apple", "banana", "cherry", "date", "grapefruit"); String longestString = strings.stream() .max(Comparator.comparingInt(String::length)).get(); System.out.println(longestString);
grapefruit
  1. Check if a array contains any prime number of not.
import java.util.List; import java.util.Arrays; public class Query { public static boolean isPrime(int number) { for (int i = 2; i < number / 2; i++) { if (number % i == 0) return false; } return true; } public static void main(String[] args) { List<Integer> numbers = Arrays.asList(29, 15, 11, 6, 13, 22, 19, 17, 21, 7); boolean primeNumberPresent = numbers.stream() .anyMatch(Query::isPrime); System.out.println(primeNumberPresent); } }
true
  1. Find the occurrence of each word on the array.
String[] words= {"apple", "banana", "apple", "orange", "banana", "apple"}; Map<String, Long> wordCount = Arrays.asList(words) .stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); System.out.println(wordCount);
{orange=1, banana=2, apple=3}

Function.identity: Returns a function that always returns its input argument.

  1. Remove duplicate from array and arrange in descending order.
List<Integer> numbers = Arrays.asList(1, 2, 3, 2, 4, 5, 1); List<Integer> distinctNum = numbers.stream().distinct() .sorted(Comparator.comparingInt(Integer::intValue).reversed()) .collect(Collectors.toList()); System.out.println(distinctNum);
[5, 4, 3, 2, 1]
  1. Given two array in sorted order, merge them in sorted order.
List<Integer> list1 = Arrays.asList(0, 3, 5, 7, 10); List<Integer> list2 = Arrays.asList(2, 4, 6, 9, 11); List<Integer> res = Stream.concat(list1.stream(), list2.stream()) .sorted().collect(Collectors.toList()); System.out.println(res);
[0, 2, 3, 4, 5, 6, 7, 9, 10, 11]
  1. Given two array, find the common item between them,
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5); List<Integer> list2 = Arrays.asList(3, 4, 5, 6, 7); List<Integer> common = list1.stream() .filter(list2::contains) .collect(Collectors.toList()); System.out.println(common);
[3, 4, 5]
  1. Find the average of item in array of type double.
List<Double> doubles = Arrays.asList(6.5, 7.3, 8.9, 9.1, 10.2, 11.4, 12.6, 13.7, 14.5, 15.3); OptionalDouble average = doubles.stream().mapToDouble(Double::doubleValue).average(); System.out.println(average); `` ```less OptionalDouble[10.95]
  1. Find kth smallest element in array.
int[] array = {20, 35, 15, 40, 5, 25, 30, 10, 50, 45}; int k = 4; int kthSmallest= Arrays.stream(array).sorted().skip(k-1).findFirst().orElse(-1); System.out.println(kthSmallest);
20
  1. Concatenate string with space in between the string.
String[] arr = {"How", "are", "you"}; String finalStr = Arrays.stream(arr).collect(Collectors.joining(" ")); System.out.println(finalStr);
How are you
  1. Given a list of integers, find the product of all non-negative integers.
List<Integer> list = Arrays.asList(4, 5, -6, 7, -1, 2, -3); long res = list.stream().filter(n -> n < 0).mapToLong(Integer::longValue).reduce(1, (a, b) -> a * b); System.out.println(res);
-18
  1. Given a list of string, find the longest palindrome string.
List<String> list = List.of("racecar", "apple", "civic", "rotator", "banana", "deified", "python", "noon", "kayak", "developer"); String longestPalindrom = list.stream() .filter(s -> new StringBuilder(s).reverse().toString().equalsIgnoreCase(s)) .max(Comparator.comparingInt(String::length)).get(); System.out.println(longestPalindrom);
racecar
  1. Given a list of string, find the string which starts with vowel.
List<String> fruits = Arrays.asList("Litchi", "apple", "mango", "pomegranate", "banana", "orange", "kiwi", "strawberry"); List<String> fruitWithVowelFirst = fruits.stream().filter(s -> "aeiou".contains(String.valueOf(s.charAt(0)))) .collect(Collectors.toList()); System.out.println(fruitWithVowelFirst);
[apple, orange]