Introduction
This article covers the new Parallel Sorting, Exact Numeric Operations and Stamped Lock features, of Java 8. The Array class of the "util" package contains various methods but, "parallelSort()" is one of the new features. Similarly, the Math class of the "lang" package contains various methods, but to get an exact value of multiplication of large data, this class has a "multiplyExact()" method. The class "locks" of the "util" package contains some new features. The "StampedLock()" method is one of them and these methods are described, in this article.
Parallel Sorting
Example: In this example, first create a list and declare some numbers. Then, create two arrays for sorting:
- import java.util.*;
- public class Parallel_Sort {
- public static void main(String[] args) {
- List < Integer > list = new ArrayList();
- Random r = new Random();
- for (int x = 0; x < 27061990; x++) {
- list.add(r.nextInt(2706));
- }
- Integer[] array1 = new Integer[list.size()];
- Integer[] array2 = new Integer[list.size()];
- array1 = list.toArray(array1);
- array2 = list.toArray(array2);
- long start = System.currentTimeMillis();
- Arrays.sort(array1);
- long end = System.currentTimeMillis();
- System.out.println("Array Sort Required Time: " + (end - start));
- start = System.currentTimeMillis();
- Arrays.parallelSort(array2);
- end = System.currentTimeMillis();
- System.out.println("Parallel Sort Required Time: " + (end - start));
- }
- }
Output
Exact Numeric Operations
The "math" package also has some features in the new version of Java. Decimal Arithmetic is of the oldest techniques and has been refined over the decades. The methods of the "math" class throw "java. lang.AirthmeticExceptios" every time the result goes to a max limit, or overflows. Let's see an example. If we multiply 100000*100000 it generally gives us wrong output. But, by using the "multiplyExact()" method, we can get over this problem because, it throws "java. lang.AirthmeticExceptios" if the result overflows the max limit. Let's see the example.
Example
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- public class oldmath_exmple {
- public static void main(String args[]) throws java.lang.ArithmeticException {
- System.out.println(100000 * 100000);
- }
- }
Output

Example
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- public class math_exmple {
- public static void main(String args[]) throws java.lang.ArithmeticException {
- System.out.println(Math.multiplyExact(100000, 100000));
- }
- }
Output
Another example
We all know about n%2 (using the modulus operator), if the number is even it gives 0. If the number is odd it gives 1. But, the problem starts when a negative even number gives -1.
Example
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- public class oldmath_exmple {
- public static void main(String args[]) throws java.lang.ArithmeticException {
- System.out.println(-27 % 2);
- System.out.println(-26 % 2);
- System.out.println(50 % 2);
- System.out.println(51 % 2);
- }
- }
Output

Example: But by using "Math.floorMod()" method we can handle this problem.



Join the conversation! Your thoughts help the community grow.