5

I work in payment gateway company and while analyzing the code I came across ImmuatbleMap objects. I want to know all the details about. What is it? What are its advantages over the map? Does core java has its support for ImmuatbleMap?

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
vaibhav gote
  • 67
  • 1
  • 1
  • 3
  • 3
    Possible duplicate of [What is meant by immutable?](http://stackoverflow.com/questions/279507/what-is-meant-by-immutable) – OH GOD SPIDERS Mar 17 '17 at 10:44
  • 3
    Have you read this https://github.com/google/guava/wiki/ImmutableCollectionsExplained ? I think that your question is too broad for been treated here. – RubioRic Mar 17 '17 at 10:48
  • 1
    try this link http://stackoverflow.com/questions/8892350/immutable-vs-unmodifiable-collection – Sumit Sagar Mar 17 '17 at 10:54
  • The main difference between `Map` and `ImmutableMap` is that the former is an interface, of which the latter is a concrete implementation. – Andy Turner Mar 17 '17 at 10:57
  • 1
    @AndyTurner Actually `ImmutableMap` is an abstract class with additional contract (immutability guarantees), concrete implementations are hidden. – Grzegorz Rożniecki Mar 17 '17 at 11:11
  • @Xaerxess OK, concrete-ish :) The point is that it implements the interface. – Andy Turner Mar 17 '17 at 11:12

1 Answers1

9

ImmutableMaps are introduced in Guava, they comply to Java's Map API, but with additional guarantees.

In a nutshell:

  • you cannot add, replace or remove entries
    • they are "fully immutable" if entry objects do not have mutable state
    • as a consequence, they are thread safe
  • nulls are forbidden
  • (slightly) more time- and space-efficient compared to usual Java's collections
  • iteration order is predictable

For full info see Guava's guide and javadoc for ImmutableCollection which applies to maps and other Guava's immutable collections.

rrobby86
  • 1,356
  • 9
  • 14
  • 2
    You might add a link to https://github.com/google/guava/wiki/ImmutableCollectionsExplained#why which also mentions "time and space savings". – mfulton26 Mar 17 '17 at 20:28