O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

You may have probably noticed the Set.of(), Map.of(), and Map.ofEntries() methods do not preserve the order of their elements. This is different from the previous (before Java 9) instances of Set and Map; the order of elements now changes between runs even on the same computer (but does not change during the same run, no matter how many times the collection is iterated). This is an intentional feature intended to help programmers avoid reliance on a certain order because it might produce a defect as the order changes when the code is run on another computer.

Another feature of the collections generated by the new of() static methods of the List, Set, and Map interfaces is that these collections are immutable. What does this ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required