Skip to content

Latest commit

 

History

History
51 lines (37 loc) · 4.39 KB

File metadata and controls

51 lines (37 loc) · 4.39 KB

Java 8+ Features

Q76. What are lambda expressions? A concise way to represent an anonymous function — an implementation of a functional interface — without boilerplate. Syntax: (parameters) -> expression or (parameters) -> { statements }. E.g., Runnable r = () -> System.out.println("run");.

Q77. What is a functional interface? An interface with exactly one abstract method (may have any number of default/static methods), enabling it to be implemented via a lambda expression. Marked (optionally) with @FunctionalInterface for compile-time verification. Examples: Runnable, Comparator<T>, Function<T,R>, Predicate<T>, Supplier<T>, Consumer<T>.

Q78. What are the core functional interfaces in java.util.function?

  • Function<T,R>: takes T, returns R (apply).
  • Predicate<T>: takes T, returns boolean (test).
  • Consumer<T>: takes T, returns nothing (accept).
  • Supplier<T>: takes nothing, returns T (get).
  • BiFunction<T,U,R>, UnaryOperator<T>, BinaryOperator<T>: variations for multiple arguments/same-type operations.

Q79. What is the Stream API? An abstraction (Java 8+) for processing sequences of elements in a functional, declarative style, supporting operations like filter, map, reduce, sorted, collect. Streams are lazy (intermediate operations don't execute until a terminal operation is invoked) and typically not reusable (consumed once).

Q80. What is the difference between intermediate and terminal stream operations? Intermediate operations (filter, map, sorted, distinct) return a new Stream and are lazily evaluated. Terminal operations (collect, forEach, reduce, count) trigger the actual processing and produce a result or side-effect, after which the stream is considered consumed.

Q81. What is the difference between map() and flatMap()? map() transforms each element into another single element (1-to-1), e.g., String → Integer. flatMap() transforms each element into a stream and flattens all resulting streams into a single stream (1-to-many), e.g., flattening a List<List<String>> into a single Stream<String>.

Q82. What is Optional, and why was it introduced? A container object (Java 8+) that may or may not hold a non-null value, used as a return type to explicitly signal "a value might be absent" — reducing NullPointerExceptions and forcing callers to handle the empty case explicitly (isPresent(), orElse(), orElseThrow(), map()). Not intended for use as a field type or method parameter.

Q83. What are default and static methods in interfaces? Introduced in Java 8 to allow adding new methods to interfaces without breaking existing implementations. Default methods (default void method() {...}) provide a body that implementing classes inherit unless overridden. Static methods belong to the interface itself and aren't inherited by implementers, called as InterfaceName.method().

Q84. What is method reference syntax? A shorthand for lambdas that just call an existing method: ClassName::methodName (static), object::instanceMethod, ClassName::instanceMethod (unbound), ClassName::new (constructor reference). E.g., list.forEach(System.out::println).

Q85. What is the new Date/Time API (java.time)? Introduced in Java 8 to replace the flawed, mutable Date/Calendar classes. Key classes: LocalDate, LocalTime, LocalDateTime (no timezone), ZonedDateTime (with timezone), Duration/Period (time spans), Instant (machine timestamp). All classes are immutable and thread-safe.

Q86. How do you collect a stream into a Map or grouped structure? Using Collectors:

Map<String, List<Person>> byCity = people.stream()
    .collect(Collectors.groupingBy(Person::getCity));

Map<String, Integer> nameToAge = people.stream()
    .collect(Collectors.toMap(Person::getName, Person::getAge));

Collectors.partitioningBy splits into two groups (true/false); Collectors.joining concatenates strings.

Q87. What is a parallel stream, and when should you avoid it? stream.parallel() splits the workload across multiple threads (via the common ForkJoinPool) to potentially process elements concurrently. Avoid it for: small datasets (overhead outweighs benefit), I/O-bound tasks, operations with shared mutable state, or when order matters and hasn't been explicitly preserved — always benchmark before adopting.