Over a million developers have joined DZone.

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

DZone's Guide to

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

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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 =LocalDate.now();
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.now();
LocalTime newTime = time.plus(p);// or time.plus(5, HOURS); or time.plusHours(5); 

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}