Sunday, August 16, 2020

Migration to Java 11

 The following section now discusses a few APIs from the JDK 11, their benefits and the preparatory work.

The Arrays api includes a faster and more powerful data structure. All the methods for manipulating arrays including sorting and searching are available as earlier. The sorting algorithm used is Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley and Joshua Bloch. This algorithm has the same O(nlogn) as in the family of quicksort algorithms but is faster than traditional ones for a broader workload.

The traditional algorithm consisted of a single pivot where all elements less than the pivot come before the element and all elements after the pivot come after it and the sub-arrays are recursively sorted.

With the dual pivot variation, there are two pivots which are chosen as say the first and the last element. The pivots have to be sorted otherwise they are swapped.  The range between the pivots is broken down into non-overlapping sub-ranges denoted by sentinels at index L, K and G progressively between the far left and the far right.

The subrange between left+1 to L –1 have elements less than P1

The subrange between L to K-1 have elements >= P1 and <= P2

The subrange between K to G can have any arbitrary remaining elements

The subrange between G+1 to right-1 have elements > P2

This way the arbitrary elements in the third sub-range above is shrunk by placing the element in one of the other three sub-ranges after comparing it with the two pivots. The L, K and G are advanced as the third sub-range is shrunk.

The first pivot element is then swapped with the last element of the first sub-range above.

The second pivot element is then swapped with the first element of the last sub-range above.

The steps are repeated recursively for each sub-range.

The key thing to note here is that for relatively large arrays the complexity remains somewhat similar but everyday programmers generally use this algorithm for small array sizes. The authors took the approach that with a threshold for array length as 27, insertion sort is preferable over all other sorting methods. In JDK 8, this threshold was set to 47.

Arrays are definitely an improvement but the methods are similar to what was available in Java 8.

On the other hand, there are new methods available as well. For example, String methods are available in String methods. For example, IsBlank() method tests if a string is empty. Lines() method splits a string based on new line character. Repeat and strip methods are similar to those other languages 

These methods add more than just convenience because the library works better than in the previous version. 

If a pattern matches with input string, the asPredicate method can create a predicate. 

JavaEE and CORBA modules were deprecated in Java 9 and they have been removed in Java 11. The thread functions of stop and destroy have been removed from Java 11.

Java 10 introduced local ‘var' syntax and Java 11 allows ‘var’ to be used in Lambda expressions.

The deployment stack pertaining to applets has been removed from JDK while this was marked deprecated in Java 9. This eliminates the need to support browsers for this stack. Java FX and Java Mission Control are available separately.


No comments:

Post a Comment