
Java 8 brought significant enhancements to the language, especially with the introduction of lambda expressions, streams, and functional programming concepts. To prepare effectively for modern Java interviews, mastering these Java coding questions based on Java 8 is vital. This blog provides 25 important programming questions along with detailed answers using Java 8 features.
Top 25 Java Coding Questions | Java Coding Questions for Practice
1. What is a Lambda Expression in Java 8?
Explanation:
A lambda expression is a short block of code which takes parameters and returns a value. It helps to implement functional interfaces easily.
Example:
Runnable r = () -> System.out.println("Hello from Lambda!");
r.run();
2. How do you use Streams to filter a collection?
Explanation:
Streams provide a filter method to select elements based on a predicate.
Example:
List<String> names = Arrays.asList("John", "Jane", "Jack");
List<String> filtered = names.stream()
.filter(name -> name.startsWith("J"))
.collect(Collectors.toList());
3. Explain the difference between map()
and flatMap()
in Streams.
Explanation:
map()
transforms each element into another object.flatMap()
flattens nested structures into a single stream.
Example:
List<List<String>> nestedList = Arrays.asList(
Arrays.asList("a", "b"),
Arrays.asList("c", "d")
);
List<String> flatList = nestedList.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
4. How to sort a list without using Collections.sort()
?
Explanation:
Java 8 added List.sort()
method accepting a Comparator.
Example:
List<Integer> numbers = Arrays.asList(3, 5, 1, 4);
numbers.sort((a, b) -> a.compareTo(b));
5. How to find the maximum or minimum element in a list using Streams?
Example:
OptionalInt max = IntStream.of(1, 5, 3).max();
OptionalInt min = IntStream.of(1, 5, 3).min();
max.ifPresent(System.out::println);min.ifPresent(System.out::println);
6. What is a Functional Interface? Give examples.
Explanation:
An interface with exactly one abstract method. Example: Runnable
, Callable
.
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
7. How to use Optional
to avoid NullPointerException?
Example:
Optional<String> optional = Optional.ofNullable(null);
optional.ifPresent(System.out::println); // Safe even if null
8. How to sum elements of a list using Streams?
Example:
int sum = Arrays.asList(1, 2, 3).stream()
.mapToInt(Integer::intValue)
.sum();
9. How to convert all strings in a list to uppercase using Streams?
Example:
List<String> upper = Arrays.asList("java", "code").stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
10. How to group elements of a list using Collectors.groupingBy()
?
Example:
List<String> names = Arrays.asList("Alice", "Adam", "Bob");
Map<Character, List<String>> grouped = names.stream()
.collect(Collectors.groupingBy(name -> name.charAt(0)));
//{A=[Alice, Adam], B=[Bob]}
11. How to remove duplicates from a list using Streams?
Example:
List<String> unique = Arrays.asList("apple", "apple", "banana").stream()
.distinct()
.collect(Collectors.toList());
12. How to find the first or any element in a Stream?
Example:
Optional<String> first = Arrays.asList("a", "b").stream().findFirst();
first.ifPresent(System.out::println);
13. How to iterate over a list using forEach
?
Example:
List<String> list = Arrays.asList("a", "b");
list.forEach(System.out::println);
14. What are method references and how are they used?
Explanation:
Method references are shortcuts for lambdas calling existing methods.
Example:
list.forEach(System.out::println);
15. How to flatten a nested list using flatMap()
?
Already answered in #3.
16. How to calculate the average of numbers using Streams?
Example:
OptionalDouble average = Arrays.asList(1, 2, 3).stream()
.mapToInt(Integer::intValue)
.average();
average.ifPresent(System.out::println);
//2.0
17. How to check if any element in a collection matches a predicate?
Example:
boolean anyMatch = Arrays.asList("apple", "banana").stream()
.anyMatch(s -> s.startsWith("b"));
18. How to generate infinite streams and limit the output?
Example:
Stream.iterate(0, n -> n + 2)
.limit(5)
.forEach(System.out::println);
19. How to join strings from a list into a single string using Collectors.joining()
?
Example:
String joined = Arrays.asList("Java", "Coding").stream()
.collect(Collectors.joining(", "));//JavaCoding
20. How to convert a Stream into an array?
Example:
String[] array = Arrays.asList("a", "b").stream()
.toArray(String[]::new);
21. How to debug streams using peek()
?
Example:
Arrays.asList("a", "bb").stream()
.peek(System.out::println)
.filter(s -> s.length() > 1)
.collect(Collectors.toList());
22. How to implement your own Functional Interface?
Answered in #6.
23. How to filter a list based on multiple conditions?
Example:
List<String> filtered = Arrays.asList("apple", "banana", "apricot").stream()
.filter(s -> s.startsWith("a") && s.length() > 5)
.collect(Collectors.toList());
24. How to use reduce()
to perform aggregation operations?
Example:
Optional<Integer> sum = Arrays.asList(1, 2, 3).stream()
.reduce(Integer::sum);
25. What are default methods in interfaces and how are they useful?
Explanation:
Default methods provide implementation inside interfaces so classes don’t have to implement them unless they want to override.
interface Vehicle {
default void print() {
System.out.println("I am a vehicle");
}
}
Conclusion
These Java Coding Questions showcase essential Java 8 features that are frequently asked in interviews and useful in daily programming. Practicing these concepts will help you write clean, efficient, and modern Java code. Keep experimenting with lambda expressions, streams, and optionals to fully leverage Java 8’s power. Download Java.
- How to Use Arrays in Java (with Code Examples for Beginners)
- Compilation in Java – How It Works with Execution Flow, Steps, and Example
- How to Use loops in Java: For, While, Do‑While Explained
- How to Use If Else Statement in Java (With Examples)
- How to Understand Object-Oriented Programming in Java Easily