This document summarizes the new features in Java 8, including lambda expressions, method references, functional interfaces, default methods, Optional class, parallel sorting, and Calendar.Builder class. Key changes are more concise coding of sorting and comparison using lambda expressions instead of anonymous classes, ability to reference methods by name, default interface methods to add functionality without changing existing classes, and parallel sorting to improve performance on large arrays.
12. Method References
• It help to point to methods by their names.
• A method reference is described
using :: (double colon) symbol.
• A method reference can be used to point the
following types of methods −
Static methods
Instance methods
Constructors using new operator (TreeSet::new)
13. Types of method reference
Type Example Syntax
1. Reference to a static
method ContainingClass::staticMethodName Class::staticMethodName
2. Reference to a constructor ClassName::new ClassName::new
3. Reference to an instance
method of an arbitrary object
of a particular type
ContainingType::methodName Class::instanceMethodName
4. Reference to an instance
method of a particular object containingObject::instanceMethodName object::instanceMethodName
14. You can call the methods with their names
(references) with :: operator.
Arrays.sort(names, Test::matchStringLength);
16. • Functional Interface have a single functionality
to exhibit.
• For example, a Comparable interface with a
single method ‘compareTo’ is used for
comparison purpose.
• Java 8 has defined a lot of functional
interfaces to be used extensively in lambda
expressions.
18. • Java 8 introduces default method so that
List/Collection interface can have a default
implementation of forEach method,
• The class implementing these interfaces need
not implement the same.
20. Multiple Defaults
• With default functions in interfaces, there is a
possibility that a class is implementing two
interfaces with same default methods.
21. public interface vehicle {
default void print(){
System.out.println("I am a vehicle!");
} }
public interface fourWheeler {
default void print(){
System.out.println("I am a four wheeler!");
} }
23. • Optional is a container object which is used to
contain not-null objects.
• Optional object is used to represent null with
absent value.
• This class has various utility methods to
facilitate code to handle values as ‘available’
or ‘not available’ instead of checking null
values.
24. Class Declaration for java.util.Optional<T>
public final class Optional<T> extends Object
26. Parallel sort
• Arrays#parallelSort uses Fork/Join framework
introduced in Java 7 to assign the sorting tasks
to multiple threads available in the thread
pool.
27. Difference between Arrays.sort &
Arrays.ParallelSort
1. Arrays.sort() : is a sequential sorting.
The API uses single thread for the operation.
The API takes bit longer time to perform the
operation.
2. Arrays.ParallelSort() : is a parallel sorting.
The API uses multiple threads.
The API takes lesser the time compared to Sort().
28. Why Parallel Sort?
• Both sort() and parallelSort() sorts the array.
• The performance with parallelSort() can be
seen when the number of arrays to sort are
very many.
32. • In JDK 1.8, Calendar.Builder is used to
instantiate calendar1 instance and all set
methods are used as a single statement.
• Semicolon is given only one
after build() method.
33. public static void DemoCalendarWithSingleSet() {
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone(timeZoneId), ENGLISH);
calendar.set(2013, APRIL, 6, 15, 45, 22);
out.println("Calendar via Constructor: " + stringifyCalendar(calendar));
}
35. What’s gone ??
• Javax.swing.ImageIcon.Component
component
@Deprecated
• protected static final Component component
• Deprecated. since 1.8
• Do not use this shared component, which is used to track
image loading. It is left for backward compatibility only.
tracker
@Deprecated
• protected static final MediaTracker tracker
• Deprecated. since 1.8
• Do not use this shared media tracker, which is used to load
images. It is left for backward compatibility only.