I wanted to create an immutable hashMap inline using the new factory method Map.ofEntries()
in Java 9, for example:
Map<Integer, String> map = Map.ofEntries(
Map.entry(1, "One"),
Map.entry(2, "Two"),
Map.entry(3, "Three"));
Then to my surprise, I found I could not create an immutable hashMap the same way! For example, the following code would not work.
HashMap<Integer, String> map = HashMap.ofEntries( //not work
Map.entry(1, "One"),
Map.entry(2, "Two"),
Map.entry(3, "Three"));
Then when I want to check what type of map is returned by the factory method, I found the following note:
Callers should make no assumptions about the identity of the returned instances.
So my question is, is the access time complexity of an immutable map the same as a hashMap which is o(1)? If not, how to create a map that is both immutable and access o(1) at the same time? It would be best if it can be created inline.