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()
.collect(Collectors.toMap(String::length, Function.identity(), (s1, s2) -> s1));
3. How do you find the first element that matches a condition?
Optional<String> result = Stream.of(“apple”, “banana”, “cherry”)
.filter(s -> s.length() > 5)
.findFirst();
4. How do you calculate the average of a list of numbers?
List<Integer> numbers = Arrays.asList(10, 20, 30, 40);
double avg = numbers.stream()
.mapToInt(Integer::intValue)
.average()
.orElse(0);
5. How do you collect elements into a set?
Set<String> uniqueWords = Stream.of(“apple”, “banana”, “apple”)
.collect(Collectors.toSet());
6. How do you sort a list of objects by a field?
List<Person> people = getPeople();
List<Person> sorted = people.stream()
.sorted(Comparator.comparing(Person::getAge))
.collect(Collectors.toList());
7. How do you check if all elements match a condition?
boolean allMatch = Stream.of(2, 4, 6, 8)
.allMatch(n -> n % 2 == 0);
8. How do you merge two lists using streams?
List<String> merged = Stream.concat(list1.stream(), list2.stream())
.collect(Collectors.toList());
9. How do you count the number of elements greater than a threshold?
long count = Stream.of(5, 10, 15, 20)
.filter(n -> n > 10)
.count();
10. How do you reduce a list of numbers to their sum?
int sum = Stream.of(1, 2, 3, 4, 5)
.reduce(0, Integer::sum);
11. How do you find duplicate elements in a list?
Set<String> seen = new HashSet<>();
Set<String> duplicates = list.stream()
.filter(e -> !seen.add(e))
.collect(Collectors.toSet());
12. How do you convert a list of strings to a comma-separated string?
String result = list.stream()
.collect(Collectors.joining(“, “));
13. How do you find the longest string in a list?
Optional<String> longest = list.stream()
.max(Comparator.comparingInt(String::length));
14. How do you skip the first N elements in a stream?
List<Integer> skipped = list.stream()
.skip(3)
.collect(Collectors.toList());
15. How do you limit the number of elements in a stream?
List<Integer> limited = list.stream()
.limit(5)
.collect(Collectors.toList());
16. How do you convert a list of strings to uppercase and sort them?
List<String> result = list.stream()
.map(String::toUpperCase)
.sorted()
.collect(Collectors.toList());
17. How do you check if any element matches a condition?
boolean hasMatch = list.stream()
.anyMatch(s -> s.contains(“test”));
18. How do you collect elements into a LinkedList?
LinkedList<String> linked = list.stream()
.collect(Collectors.toCollection(LinkedList::new));
19. How do you find the sum of squares of a list of integers?
int sum = list.stream()
.map(n -> n * n)
.reduce(0, Integer::sum);
20. How do you convert a map to a list of keys using streams?
List<String> keys = map.entrySet().stream()
.map(Map.Entry::getKey)
.collect(Collectors.toList());
Search
Categories
Recent Posts
Top Git interview Questions for Experienced Developer
Speed-up tips and tricks for Windows 11
Top 15 Linux Commands used by Experienced Developer
Top SQL Interview Examples for Experienced Developer
Internal Working of Java HashMap
Recent Tags