1. Features of Java 8


2. Functional Interfaces

Java 8 introduced several built-in functional interfaces in the java.util.function package to facilitate functional programming:

  1. Supplier<T>
  2. Consumer<T>
  3. Predicate<T>
  4. Function<T, R>
  5. UnaryOperator<T>
  6. BinaryOperator<T>

3. Lambda Expressions

Lambda expressions in Java provide a way to write shorter and more concise code for defining small, single-use functions, often referred to as anonymous functions.

They allow you to:


4. Lambda Expressions – Examples

Traditional approach using an anonymous inner class:

javaCopyEditRunnable runnable = new Runnable() {
    @Override
    public void run() {
        System.out.println("Hello, World!");
    }
};

Using a lambda expression:

javaCopyEditRunnable runnable = () -> {
    System.out.println("Hello, World!");
};

5. Stream API

The Stream API facilitates functional-style operations on sequences of elements. It allows:


6. Stream API – Examples

javaCopyEditList<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve");

// Filter names starting with 'A'
List<String> filteredNames = names.stream()
    .filter(name -> name.startsWith("A"))
    .collect(Collectors.toList());

// Convert names to uppercase
List<String> uppercaseNames = names.stream()
    .map(String::toUpperCase)
    .collect(Collectors.toList());

// Sum of all numbers
int sum = numbers.stream()
    .reduce(0, Integer::sum);

// Sort names alphabetically
List<String> sortedNames = names.stream()
    .sorted()
    .collect(Collectors.toList());

7. Default Methods

Before Java 8, interfaces could only contain method declarations with no implementations. Java 8 introduced default methods to interfaces, enabling:


8. Date and Time API

Java 8 introduced a new Date and Time API in the java.time package to overcome the limitations of java.util.Date and java.util.Calendar.

Key Features:

Default Methods in Interfaces (Java 8)

Definition:
A default method in an interface is a method with a body (implementation). It allows interfaces to have methods with default behavior without affecting implementing classes.

Example:

javaCopyEditinterface Vehicle {
  // Regular method (abstract)
  void start();

  // Default method with implementation
  default void honk() {
    System.out.println("Honk honk!");
  }
}

class Car implements Vehicle {
  // Implementing abstract method
  public void start() {
    System.out.println("Car started.");
  }
}

Method Reference (Java 8)

Definition:
A method reference is a shorthand notation of a lambda expression to call a method directly.

Types of Method References:

  1. Static MethodClassName::staticMethodName
  2. Instance Method of a Particular Objectinstance::methodName
  3. Instance Method of an Arbitrary Object of a Particular TypeClassName::methodName
  4. Constructor ReferenceClassName::new

Example:

javaCopyEdit// Instance method to print string in lowercase
public void printLowerCase(String str) {
  System.out.println(str.toLowerCase());
}

// Reference to an instance method of a particular object
MethodReferenceExample converter = new MethodReferenceExample();
names.forEach(converter::printLowerCase);

Optional Class (Java 8)

Purpose:
To handle values that might be null and avoid NullPointerException.

Key Points:

Example:

javaCopyEditString str = "Hello";
Optional<String> optionalStr = Optional.ofNullable(str);

// Check presence
if (optionalStr.isPresent()) {
  System.out.println("Value is present: " + optionalStr.get());
} else {
  System.out.println("Value is absent");
}

// Using ifPresent
optionalStr.ifPresent(value ->
  System.out.println("Value is present: " + value)
);

// Using orElse
String defaultValue = optionalStr.orElse("Default Value");
System.out.println("Value or default: " + defaultValue);

forEach() Method (Java 8)

Definition:
Used to perform an action for each element of a collection.

Available In:

Usage Example:

javaCopyEditList<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(name -> System.out.println(name));

Nashorn JavaScript Engine (Java 8)

Overview:


Parallel Array Sorting (Java 8)

Method: Arrays.parallelSort()

Key Benefits:

Example:

javaCopyEditint[] numbers = {5, 3, 8, 1, 2};
Arrays.parallelSort(numbers);

Leave a Reply

Your email address will not be published. Required fields are marked *