Java 9 for Competitive Programming

Revision en3, by limed, 2017-09-23 22:20:52

As Java 9 has been released few days ago, I went through the What's New to see if there is something useful in the context of Competitive Programming. And I didn't find much, just these few enhancements:

  • new static factory methods on the List, Set, and Map interfaces (returning immutable instances of those collections), for example: Set<String> alphabet = Set.of("a", "b", "c");

  • internal String representation as array of bytes (8 bit) instead of array of chars (16 bit) in case the string contains only Latin-1 characters (which is normally the case in Competitive Programming); this would reduce memory consumption almost by half in String problems, and I wonder what would be the impact on performance;

  • JShell (aka REPL in some other languages) for executing small pieces of code / bruteforce solutions (thanks bhishma for suggesting this).

Also, there are some new standard library methods not mentioned in "What's new" (thanks drinkless for pointing this out). Here's what I found:

  • Math.fma(a, b, c) to compute a * b + c on doubles/floats with better performance and precision;

  • A few new Math.multiplyExact/multiplyFull/multiplyHigh methods;

  • Arrays.equals to compare two subarrays;

  • Arrays.compare to compare two arrays/subarrays lexicographically;

  • Arrays.mismatch to compute common prefix length of two arrays.

(Array methods should have efficient implementations using vectorised CPU instructions).

As a personal note, I feel like Java is still light years behind Scala (which unfortunately is broken on CF for 2 months already) for writing CP code, even though both of them run on the same JVM. Just please, don't start language flame wars, unless you have tried both :)

P. S. If someone goes through "What's new" of Java 9 and notices some more relevant enhancements, please post them here. I could have missed something.

Tags java, string, scala

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English limed 2017-09-23 22:20:52 1173 Added some more Java 9 features relevant to CP.
en2 English limed 2017-09-23 14:22:00 153 Tiny change: 'h :)\n\nP.S. If some' -> 'h :)\n\nP. S. If some'
en1 English limed 2017-09-23 14:15:29 1082 Initial revision (published)