Java 8: Conceptual Questions

Conceptual Questions

1. What are the main features introduced in Java 8?

  • Lambda Expressions: Enables functional programming by writing functions inline.
  • Stream API: Provides functional-style operations on collections.
  • Functional Interfaces: Interfaces with a single abstract method (e.g., PredicateFunction).
  • Optional: Helps avoid NullPointerException.
  • Default Methods: Allows adding default implementations in interfaces.
  • Date and Time API: Improved date and time handling.
  • Method References: Simplifies calling methods using :: operator.

2. What are functional interfaces?

  • Functional interfaces contain exactly one abstract method.
  • They support lambda expressions and method references.
  • Examples:
    • Runnable (method: void run())
    • Predicate<T> (method: boolean test(T t))
    • Function<T, R> (method: R apply(T t))

3. Explain Stream and its key methods.

  • Stream represents a sequence of elements for processing.
  • Intermediate Operations (return a Stream):
    • filter(): Filters elements based on a condition.
    • map(): Transforms elements.
    • sorted(): Sorts elements.
  • Terminal Operations (consume the Stream):
    • collect(): Converts to a collection.
    • forEach(): Performs an action.
    • reduce(): Aggregates elements.

4. What is the difference between map() and flatMap()?

  • map(): Transforms each element, resulting in a stream of streams.
  • flatMap(): Flattens and transforms nested structures into a single stream.

5. What is Optional in Java 8?

  • A container object used to avoid NullPointerException.
  • Key methods:
    • Optional.of(value): Creates an Optional with a non-null value.
    • Optional.empty(): Creates an empty Optional.
    • ifPresent(): Performs an action if a value is present.

6. How do default methods work in interfaces?

  • They allow adding new methods to interfaces with a default implementation.
  • Example:
interface MyInterface {
    default void show() {
        System.out.println("Default Method");
    }
}

7. What is the purpose of Collectors?

  • Collectors is a utility for reducing Stream results.
  • Common collectors:
    • toList()toSet(): Convert to a list or set.
    • joining(): Concatenates strings.
    • groupingBy(): Groups elements by a key.
    • partitioningBy(): Partitions elements into two groups.

8. How does the Date and Time API differ from java.util.Date?

  • Immutable and thread-safe classes: LocalDateLocalTimeLocalDateTime.
  • DateTimeFormatter for parsing and formatting.
  • Zone-aware classes like ZonedDateTime.

9. What are method references in Java 8?

  • A shorthand for lambda expressions using :: operator.
  • Types:
    • Static MethodsClass::methodName
    • Instance Methodsinstance::methodName
    • ConstructorsClassName::new

10. What is parallelStream() in Java 8?

  • Processes elements in parallel for better performance with large datasets.
  • Example:
List<Integer> numbers = Arrays.asList(1, 2, 3);
numbers.parallelStream()
       .map(n -> n * 2)
       .forEach(System.out::println);

Leave a Comment