Over a million developers have joined DZone.

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

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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); 

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}