Java 8 or JDK 1.8 is the most
significant expansion of the Java language yet. Java 8’s new features such as
Lambda Expressions, Stream APIs, Nashorn, Compact Profiles, new Time APIs
increase the expressive power of the platform and make it easier for developers
to take advantage of modern, multicore processors. This article gives an
overview of the new features in Java 8 with links to in-depth tutorials for the
most important of them.
- Lambda Expressions enable you to treat functionality as a method argument, or code as data. Lambda expressions let you express instances of single-method interfaces (referred to as Functional Interfaces) more compactly.
- Method references provide easy-to-read lambda expressions for methods that already have a name. Constructor References are the equivalent forms of representation for constructors.
- Default methods enable new functionality to be added to the interfaces of libraries and ensure binary compatibility with code written for older versions of those interfaces.
- With default methods in Java 8, multiple inheritance of behavior is now possible in Java, and it is important to understand the conflict resolution rules which resolve Diamond Problem and other conflict scenarios.
Java 8’s new package
java.util.function provides many useful functional interfaces for the most
common scenarios. The 4 most important functional interface among them are –
Predicate, Consumer, Function and Supplier.
Repeating Annotations provide the
ability to apply the same annotation type more than once to the same
declaration or type use.
- New java.util.stream package provides a new Streams API
to support functional-style operations on streams of elements. The Stream API is integrated into the Collections API. - Java 8’s new Collector interface and its multiple predefined implementations provide an efficient way to terminate the Stream operations and collect the result in a collection.
- A new Date-Time package – java.time – with a new comprehensive set of date and time utilities.
- Java 8’s new enhanced methods in Collections API are covered in a series of 4 tutorials – Iterable.forEach() & Iterator.remove(), Collection.removeIf(), List.sort() & List.replaceAll(), and new multi-value map methods.
- Java 8 has introduced new internal iterators based on declarative functional programming style.
- Comparator interface has undergone a major upgrade in Java 8 with new methods leveraging Java 8’s functional programming features, comparator chaining, in-built null handling, and many more such features. Java 8 Comparator tutorial covers these new features in depth.
- Nashorn Javascript Engine enhanced to provide a version of javascript which would run within the JVM
- Standard Encoding and Decoding Base64,Parallel Array Sorting and Unsigned Arithematic Support.
- JDBC 4.2 with new features and notably the JDBC-ODBC bridge has been removed.
- Concurrency related important changes which include –
- Changes to ConcurrentHashMap to support aggregate operations based on the newly added streams facility and lambda expression.
- Addition of classes to the java.util.concurrent.atomic package to support scalable updatable variables.
- Support for a common pool in ForkJoinPool.
- New StampedLock class to to provide a capability-based lock with three modes for controlling read/write access
- Type Annotations provide the ability to apply an annotation anywhere a type is used, not just on a declaration. Used with a pluggable type system, this feature enables improved type checking of your code.
- Improved Type Inference and Method Parameter Reflection.
- Compact Profiles contain predefined subsets of the Java SE platform and enable applications that do not require the entire Platform to be deployed and run on small devices.
- Improved internationalization including support for Unicode 6.2.0, new Calendar and Locale APIs, Adoption of Unicode CLDR Data and the java.locale.providers System Property and Ability to Install a Custom Resource Bundle as an Extension.
- In case you are interesting in knowing about the best books on Java 8 then you can read the.
The
Complete Java 8 Comparator
Java 8’s Comparator is a Functional
Interface
Owing to the fact that the Comparator interface has just one abstract method, compare(), it automatically qualifies to be a Functional Interface
in Java 8. Nevertheless, Java 8 designers have gone ahead and annotated the Comparator class interface @FunctionalInterface to enforce its role as a Functional Interface. Being a functional interface, Comparator can now be used as an assignment target for a lambda expression or a method reference.
Owing to the fact that the Comparator interface has just one abstract method, compare(), it automatically qualifies to be a Functional Interface
in Java 8. Nevertheless, Java 8 designers have gone ahead and annotated the Comparator class interface @FunctionalInterface to enforce its role as a Functional Interface. Being a functional interface, Comparator can now be used as an assignment target for a lambda expression or a method reference.
Java 8 Comparator’s null handling
using nullsFirst()and nullsLast() methods
There are instances where the sort key value is nullable. We have to then decide whether we want to place objects with sort key as null earlier in the ordering than the objects with non-null sort key or at the end of the list after the objects with non-null sort key. Java 8 Comparators provide nullsFirst() and nullsLast() static methods for exactly such sort order handling of null valued sort keys. These methods have the signature –
There are instances where the sort key value is nullable. We have to then decide whether we want to place objects with sort key as null earlier in the ordering than the objects with non-null sort key or at the end of the list after the objects with non-null sort key. Java 8 Comparators provide nullsFirst() and nullsLast() static methods for exactly such sort order handling of null valued sort keys. These methods have the signature –
static
<T> Comparator<T> nullsFirst(Comparator<? super T>
comparator)
AND
static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)
AND
static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)
To understand the functioning of
these methods, let us make the two of the employee names as null. The below two
sections of code and respective output shows how the use of nullsFirst() and nullsLast() handles sorting when some objects have sort-key as null.
No comments:
Post a Comment