Performance Zone is brought to you in partnership with:

Darshan has posted 5 posts at DZone. View Full User Profile

Java 8 added Parallel array sorting and new Date & time API

  • submit to reddit
Java 8 may be the most anticipated version of Java ever. Originally slated for release in September, Java 8 has been delayed until March of next year, supposedly to buy time to make security fixes aimed mainly at client-side Java (JavaFX/Swing). Since I, like most of you, stopped caring about client-side Java shortly after Duke finally finished jumping rope, we won't address any of that.

Parallel array sorting

Java 8 introduces a new API for sorting: Arrays#parallelSort.

Arrays#parallelSort uses Fork/Join framework introduced in Java 7 to assign the sorting tasks to multiple threads available in the thread pool.

The different methods available for parallelSort are:

parallelSort(byte[] a)
parallelSort(byte[] a,int fromIndex,int toIndex)
parallelSort(char[] a)
parallelSort(char[] a,int fromIndex,int toIndex)
parallelSort(double[] a)
parallelSort(double[] a,int fromIndex,int toIndex)
parallelSort(float[] a)
parallelSort(float[] a,int fromIndex,int toIndex)
parallelSort(int[] a)
parallelSort(int[] a,int fromIndex,int toIndex)
parallelSort(long[] a)
parallelSort(long[] a,int fromIndex,int toIndex)
parallelSort(short[] a)
parallelSort(short[] a,int fromIndex,int toIndex)
parallelSort(T[] a)
parallelSort(T[] a,Comparator<?super T> c)
parallelSort(T[] a,int fromIndex,int toIndex)
parallelSort(T[] a,int fromIndex,int toIndex,Comparator<?super T> c)

Check out the Arrays javadoc for more info.

An interesting article compares Arrays.sort vs Arrays.parallelSort. It turns out parallelSort might not be better than sort if the number of element to sort is quite small which is to be expected.

New Date & Time API

The built-in date and time handling in Java is not always very convenient to use. In general, if you have to deal with date/time, you would often resort to the use of the Joda Time library. So in Java 8 a new built-in API has been made which is a lot more nice to use.

The current time is represented by the javax.time.Clock class. The class is abstract, so you can not create instances of it.

Below are a few code examples.

Clock clock =Clock.systemUTC();//return the current time based on your system clock and set to UTC.

Clock clock =Clock.systemDefaultZone();//return time based on system clock zone 

long time = clock.millis();//time in milliseconds from January 1st, 1970 

You'll deal with timezones by using the javax.time.ZoneId class.
ZoneId zone =ZoneId.systemDefault();//get the ZoneId of the system 
Clock clock =Clock.system(zone);//set the Clock to given zone 

ZoneId zone =ZoneId.of("Europe/Berlin");//get the ZoneId from timezone name 

To deal with "human" date and time, you'll use LocalDate, LocalTime and LocalDateTime.
LocalDate date;
String year = date.getYear();
String month = date.getMonthValue();
String day = date.getDayOfMonth();

To do calculations with dates, it is also very easy. Probably the best improvement compared to the current situation with Java < 1.8

Period p =Period.of(2, HOURS);
LocalTime time;
LocalTime newTime =;// or, HOURS); or time.plusHours(5); 
Published at DZone with permission of its author, Darshan Bobra.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Serge Bureau replied on Tue, 2014/01/14 - 5:47pm

Please keep your opinion on Java Client Side for yourself

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.