Java8 Stream API Commonly asked Interview questions Part 2

Abhishek vt
3 min readApr 28, 2024

--

Greetings everyone! Today, we’re exploring top interview questions concerning the Java Stream API, providing clear explanations alongside straightforward examples. These resources are specifically designed to aid individuals preparing for interviews. Whether you’re navigating technical interviews or seeking to enhance your understanding, this series aims to be a valuable asset in your journey toward success.

  1. Count the occurrences of each word in a Array of strings using streams.
 public static void main(String[] args) {


String[] words= {"apple", "banana", "apple", "orange", "banana", "apple"};

Map<String, Long> collect = Arrays.asList(words).stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));

System.out.println(collect);

}

}

2. Write a program to find the longest string in a list of strings using streams.

public static void main(String[] args) {


List<String> list = Arrays.asList("apple", "banana", "orange", "kiwi", "strawberry");

Optional<String> max = list.stream().max(Comparator.comparingInt(String::length));

System.out.println(max.get());

}

3. Given a list of integers, remove duplicates and keep them in the descending order using streams.

 public static void main(String[] args) {


List<Integer> numbers = Arrays.asList(1, 2, 3, 2, 4, 5, 1);

List<Integer> collect = numbers.stream().distinct().sorted(Comparator.comparingInt(Integer::intValue).reversed()).collect(Collectors.toList());

System.out.println(collect);


}

4. Write a program to find the average of a list of doubles using streams.

 public static void main(String[] args) {


List<Double> doubles = Arrays.asList(1.2, 3.5, 2.8, 4.1, 5.7);
OptionalDouble average = doubles.stream().mapToDouble(Double::doubleValue).average();
System.out.println(average.getAsDouble());


}

5. Merge two lists of integers and remove duplicates using streams.

 public static void main(String[] args) {

List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(3, 4, 5);

List<Integer> collect = Stream.concat(list1.stream(), list2.stream()).distinct().collect(Collectors.toList());
System.out.println(collect);

}

6. Given a list of strings, concatenate them into a single string using streams.

 public static void main(String[] args) {

List<String> list = Arrays.asList("Hello", " ", "world", "!");

String collect = list.stream().collect(Collectors.joining());
System.out.println(collect);

}

7. Write a program to find the first non-repeating character in a string using streams.

 public static void main(String[] args) {


String str = "abacdbef";


Optional<Character> firstNonRepeatingChar = str.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
.entrySet()
.stream()
.filter(e -> e.getValue() == 1L)
.map(Map.Entry::getKey)
.findFirst();


System.out.println(firstNonRepeatingChar.get());

}

8. Given a list of strings, remove all strings that contain a specific character using streams.

     List<String> list = Arrays.asList("apple", "banana", "orange", "kiwi");
char specificChar = 'a';

List<String> collect = list.stream().filter(s->!s.contains(String.valueOf(specificChar))).collect(Collectors.toList());

System.out.println(collect);

9. Given a list of integers, partition them into two groups: odd and even, using streams.

 public static void main(String[] args) {



List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
Map<Boolean, List<Integer>> oddEvenPartition = numbers.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));

System.out.println(oddEvenPartition);


}

10. Given an array of integers, find the kth largest element.

public static void main(String[] args) {

List<Integer> list = Arrays.asList(1, 12, 44, 32, 52, 81, 59, 84, 72, 37);

int k = 4;

Integer num = list.stream().sorted(Comparator.reverseOrder()).limit(k).skip(k - 1).findFirst().orElse(-1);
System.out.println(num);

}

11. Write a program to perform cube on list elements and filter numbers greater than 50

 public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(4,5,6,7,1,2,3);
integerList.stream()
.map(i -> i*i*i)
.filter(i -> i>50)
.forEach(System.out::println);
}

12. Given a list of strings, find the count of strings starting with a vowels.

public static void main(String[] args) {

List<String> list = Arrays.asList("apple", "banana", "orange", "kiwi", "strawberry");
long count = list.stream().filter(s -> "aeiouAEIOU".contains(String.valueOf(s.charAt(0)))).count();
System.out.println(count);

}

13.Given a list of strings, find the longest palindrome string.


public static void main(String[] args) {

List<String> list = List.of("level", "hello", "radar", "world", "madam", "java", "Malayalam");

String str = list.stream().filter(s -> new StringBuilder(s).reverse().toString().equalsIgnoreCase(s))
.max(Comparator.comparingInt(String::length)).orElse("");

System.out.println(str);

}

14. Given a list of integers, find the product of all non-negative integers.

 public static void main(String[] args) {

List<Integer> integerList = Arrays.asList(4, 5, -6, 7, -1, 2, -3);

long longNumber = integerList.stream().filter(num -> num >= 0).mapToLong(Integer::longValue).reduce(1, (a, b) -> a * b);

System.out.println(longNumber);

}

Thank you for taking the time to read my article until the end. I sincerely hope that you have gained valuable insights and knowledge from it. If you found the article enjoyable and informative, I kindly ask you to share it with your friends and colleagues.

If you enjoy this article, kindly consider following, subscribing, and giving it a clap.

Please take a look at my other articles.

--

--

Abhishek vt

👨‍💻Java & Blockchain Developer 🌐 | Trainer 📚 | Interview Coach 🎥 | Sharing tutorials and tips on Java ☕️ & Blockchain tech | www.abhishekvt.com