Hackforge Academy

Category: java

Example for java 8 features

Published on 22 Apr 2026

Explanation

Lambda expressions reduce boilerplate code when implementing functional interfaces. Real-time example: sorting employee objects in a list.

Code:

List<String> names = 
Arrays.asList("Ram", "John", "Anu");
names.sort((a, b) -> a.compareTo(b));

Explanation

Stream API helps process collections efficiently. Real-time example: filtering active users from a database result list.

Code:

List<String> users = 
Arrays.asList("Ram", "Inactive", "John");
users.stream()
     .filter(u -> !u.equals("Inactive"))
     .forEach(System.out::println);

Explanation

forEach method simplifies iteration over collections. Real-time example: printing order IDs in an e-commerce system.

Code:

List<Integer> orderIds = 
Arrays.asList(101, 102, 103);
orderIds.forEach(id -> 
System.out.println(id));

Explanation

Optional class avoids NullPointerException. Real-time example: safely handling nullable user email from database.

Code:

Optional<String> email = 
Optional.ofNullable(null);
System.out.println(email.orElse(
"Email not available"
));

Explanation

Functional interfaces allow passing behavior as parameters. Real-time example: validating user input using Predicate.

Code:

Predicate<String> isValid = name ->
 name.length() > 3;
System.out.println(isValid.test("Ram"));

Explanation

Method references improve readability of lambda expressions. Real-time example: logging system output.

Code:

List<String> logs = Arrays.asList(
"Start", "Process", "End");
logs.forEach(System.out::println);

Explanation

Default methods in interfaces allow adding new functionality without breaking existing implementations. Real-time example: adding logging support to service interfaces.

Code:

interface Logger {
  default void log() {
    System.out.println("Logging enabled");
  }
}

Explanation

Date and Time API improves handling of timestamps. Real-time example: recording order creation time.

Code:

LocalDateTime now = LocalDateTime.now();
System.out.println(now);

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community