0% found this document useful (0 votes)
10 views

Java 8 Notes

Interview

Uploaded by

sristi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java 8 Notes

Interview

Uploaded by

sristi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java 8 Notes

1. Lambda Expressions:

Lambda expressions introduce a clear and concise way to represent one method interface using an

expression. They are similar to closures.

Example:

List<String> names = Arrays.asList("John", "Jane", "Tom");

names.forEach(n -> System.out.println(n));

2. Functional Interfaces:

Functional interfaces have a single abstract method and are annotated with @FunctionalInterface.

Examples:

Predicate<T>, Function<T, R>, Consumer<T>, Supplier<T>

3. Streams API:

Provides a new abstraction to process sequences of data. Supports functional-style operations on

streams of elements, such as map, filter, and reduce.

Example:

names.stream().filter(n -> n.startsWith("J")).sorted().forEach(System.out::println);

4. Method References:

A shorthand notation of a lambda expression to call a method. Syntax is ClassName::methodName.

Example: names.forEach(System.out::println);

5. Default and Static Methods in Interfaces:


Interfaces can have default methods (with implementation) and static methods.

Example:

default void start() { System.out.println("Vehicle started"); }

6. Optional Class:

Used to avoid null checks and NullPointerExceptions.

Example:

Optional<String> name = Optional.ofNullable(null); name.orElse("Default Name");

7. New Date and Time API:

The java.time package provides classes such as LocalDate, LocalTime, LocalDateTime, and

ZonedDateTime to handle date and time.

Example: LocalDate today = LocalDate.now();

8. Streams Collectors:

Collectors are used to combine the elements of a stream into a collection.

Example: names.stream().filter(n -> n.startsWith("J")).collect(Collectors.toList());

9. Parallel Streams:

Streams can be processed in parallel to improve performance.

Example: names.parallelStream().forEach(System.out::println);

10. Type Annotations:

Annotations can be applied to any type.

Example: public class MyClass<@NonNull T> { }


11. Nashorn JavaScript Engine:

Java 8 provides a lightweight JavaScript engine for executing JS on the JVM.

Example: engine.eval("print('Hello from JavaScript')");

12. CompletableFuture:

A new class for asynchronous programming in Java 8.

Example: CompletableFuture.supplyAsync(() -> "Hello").thenApply(str -> str + "

World").thenAccept(System.out::println);

You might also like