Category: Java8

Top Java8 Stream Grouping by Interview Questions with examples

  • June 20, 2025
  • No Comments

1. How do you group strings by their length? Map<Integer, List<String>> grouped = list.stream()   .collect(Collectors.groupingBy(String::length)); 2. How do you group numbers by even and odd? Map<String, List<Integer>> grouped = numbers.stream()   .collect(Collectors.groupingBy(n -> n % 2 == 0 ? “Even” : “Odd”)); 3. How do you group employees by department? Map<String, List<Employee>> grouped = employees.stream()   .collect(Collectors.groupingBy(Employee::getDepartment)); […]

Top Java8 Streams Interview Questions

  • June 20, 2025
  • No Comments

1. How do you filter strings that start with a specific letter? List<String> names = Arrays.asList(“Alice”, “Bob”, “Amanda”, “Brian”); List<String> filtered = names.stream()   .filter(name -> name.startsWith(“A”))   .collect(Collectors.toList()); 2. How do you convert a list of strings to a map with string length as key? List<String> words = Arrays.asList(“Java”, “Stream”, “Lambda”); Map<Integer, String> map = words.stream() […]