Certainly! Here are the top 10 Java 8 interview coding questions and answers:
1. Question: Find the sum of all even numbers in a list using Java 8 streams.
Answer:
1 2 3 4 5 6 7 |
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); int sum = numbers.stream() .filter(n -> n % 2 == 0) .mapToInt(Integer::intValue) .sum(); System.out.println("Sum of even numbers: " + sum); |
2. Question: Calculate the square of each number in a list using Java 8 streams.
Answer:
1 2 3 4 5 6 |
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); List<Integer> squares = numbers.stream() .map(n -> n * n) .collect(Collectors.toList()); System.out.println("Squares: " + squares); |
3. Question: Remove duplicate elements from a list using Java 8 streams.
Answer:
1 2 3 4 5 6 |
List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5); List<Integer> distinctNumbers = numbers.stream() .distinct() .collect(Collectors.toList()); System.out.println("Distinct numbers: " + distinctNumbers); |
4. Question: Count the number of empty strings in a list of strings using Java 8 streams.
Answer:
1 2 3 4 5 6 |
List<String> strings = Arrays.asList("Hello", "", "World", "", "Java"); long count = strings.stream() .filter(s -> s.isEmpty()) .count(); System.out.println("Number of empty strings: " + count); |
5. Question: Convert a list of strings to uppercase using Java 8 streams.
Answer:
1 2 3 4 5 6 |
List<String> strings = Arrays.asList("apple", "banana", "cherry"); List<String> uppercaseStrings = strings.stream() .map(String::toUpperCase) .collect(Collectors.toList()); System.out.println("Uppercase strings: " + uppercaseStrings); |
6. Question: Sort a list of strings in descending order using Java 8 streams.
Answer:
1 2 3 4 5 6 |
List<String> strings = Arrays.asList("apple", "banana", "cherry"); List<String> sortedStrings = strings.stream() .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); System.out.println("Sorted strings (descending order): " + sortedStrings); |
7. Question: Find the maximum value from a list of integers using Java 8 streams.
Answer:
1 2 3 4 5 |
List<Integer> numbers = Arrays.asList(10, 5, 8, 20, 15); Optional<Integer> max = numbers.stream() .max(Integer::compareTo); System.out.println("Maximum value: " + max.orElse(0)); |
8. Question: Check if all elements in a list of strings contain the letter ‘a’ using Java 8 streams.
Answer:
1 2 3 4 5 |
List<String> strings = Arrays.asList("apple", "banana", "cherry"); boolean allContainA = strings.stream() .allMatch(s -> s.contains("a")); System.out.println("All elements contain 'a': " + allContainA); |
9. Question: Group a list of persons by their age using Java 8 streams.
Answer:
1 2 3 4 5 |
List<Person> persons = // List of Person objects with age property Map<Integer, List<Person>> groupedByAge = persons.stream() .collect(Collectors.groupingBy(Person::getAge)); System.out.println("Persons grouped by age: " + groupedByAge); |
10. Question: Calculate the average of a list of double values using Java 8 streams.
Answer:
1 2 3 4 5 6 7 |
List<Double> values = Arrays.asList(1.5, 2.5, 3.5, 4.5, 5.5); double average = values.stream() .mapToDouble(Double::doubleValue) .average() .orElse(0.0); System.out.println("Average: " + average); |
These questions and answers are intended to test your knowledge of Java 8 features, particularly streams and lambdas. Practice and understand these concepts thoroughly to perform well in Java 8 interviews.