Java 8 is one of the most important releases in the history of Java. Released on March 18, 2014, it introduced several major language and library enhancements that made Java programming more concise, expressive, and suitable for functional-style programming. The most significant Java 8 features include Lambda Expressions, Functional Interfaces, Method References, Stream API, the Optional class, the new Date and Time API, and enhancements to interfaces and collections.
- Introduction of the Stream API for efficient data processing.
- A new Date and Time API for better handling of time and date operations.
- Default and static methods in interfaces for greater flexibility.
Lambda Expressions
A Lambda Expression is used to provide the implementation of a functional interface. It enables you to write cleaner and shorter code.
- Introduction
- Lambda Expressions Parameters
- Java Lambda Expression with Collections
- Lambda Expression Variable Capturing
- Create a Thread using Lambda Expressions
- Serialization of Lambda Expression
- Block Lambda Expressions
Functional Interfaces
An interface that contains only one abstract method is known as a functional interface; however, there is no restriction on the number of default and static methods that can be included within a functional interface.
- Introduction
- Consumer Interface
- BiConsumer Interface
- Predicate Interface
- Function Interface
- Supplier Interface
Method Reference
Method reference is a shorthand notation of a lambda expression to call a method.
Streams
Stream API is introduced in Java 8 and is used to process collections of objects with the functional style of coding using the lambda expression. So to understand what stream API is, you must have knowledge of both lambda and functional interfaces.
- Introduction
- Implement Filter Function using Reduce
- Stream API Filters
- Parallel vs Sequential Stream
- Array to Stream
- Print Elements of a Stream
- Convert an Iterable to Stream
- Convert an Iterator to Stream
- Convert Stream to Set
- Convert a Set to Stream
Comparable and Comparator
Comparable and Comparator are interfaces used to order objects. They are particularly useful in sorting operations and collections that require natural ordering. Here we will learn about Comparable and Comparator in depth.
- Comparable vs Comparator
- Comparator Interface
- Sort an Array of Triplet using Java Comparable and Comparator
- Sort LinkedList using Comparable
- Sort HashSet Elements using Comparable Interface
- Sort LinkedHashMap by Values using Comparable Interface
- Sort LinkedHashMap by Keys using Comparable Interface
Optional Class
Optional is a container object that may or may not contain a non-null value. It can be used to make the absence of a value explicit and reduce some common null-handling problems.
- Introduction
- ofNullable() Method
- ifPresentOrElse() Method
- orElseGet() Method
- filter() Method
- hashCode() Method
- toString() Method
- equals() Method
- stream() Method
Date/Time API
Java 8 introduced the java.time API, which provides modern classes for working with dates, times, time zones, offsets, periods, and related operations. It provides a more comprehensive API than the older date and calendar classes.
- Introduction
- LocalDateTime Class
- MonthDay Class
- OffsetTime Class
- OffsetDateTime Class
- Clock Class
- ZonedDateTime Class
- ZoneOffset Class
- Year Class
- YearMonth Class
- Period Class
Other Java 8 Features
JaJava 8 also introduced several enhancements to interfaces, collections, and functional interfaces. These features extend the capabilities of existing Java APIs and make functional-style programming easier.
Java 8 Interface and Collection Enhancements
Java 8 introduced default and static methods in interfaces and added several useful methods to collection-related APIs.
Functional Interfaces – Unary Operator
Unary operator interfaces represent operations that accept one argument and return a value of the same type.
- LongUnaryOperator Interface
- IntUnaryOperator Interface
- DoubleUnaryOperator Interface
- UnaryOperator Interface
Functional Interfaces – Consumers and Suppliers
Java provides specialized functional interfaces for primitive types to avoid unnecessary boxing and unboxing.
- LongConsumer Interface
- ObjLongConsumer Interface
- ObjIntConsumer Interface
- ObjDoubleConsumer Interface
- DoubleConsumer
- IntSupplier Interface
- BooleanSupplier Interface
- LongSupplier Interface
- DoubleSupplier Interface
Functional Interfaces – Functions and BiFunctions
Java 8 also provides specialized function interfaces for primitive values and functions that accept two arguments.