In this tutorial I code a Google Guava ImmutableList Example and explain the immutable collection.
Guava’s ImmutableList is built on ImmutableCollection, A collection whose contents will never change and offers many more guarantees. (You cannot add, remove etc – but the elements within the collection also need to also be made immutable I.e. make all the object’s fields immutable).
Guarantees
Shallow immutability
Elements can never be added, removed or replaced in this collection. This is a stronger guarantee than that of Collections.unmodifiableCollection(java.util.Collection whose contents change whenever the wrapped collection is modified. E.g. When you create an ImmutableList using copyOf method, the copyOf collection reference is not maintained, but rather a pointer to all the contained objects at the time of copyOf invocation. This is not the same for Collections.unmodifiableCollection, that simply wraps the collection.
Elements cannot be null. Explicit null checks on construction.
Thread Safe
Can be shared between threads knowing that elements cannot be added/removed.
Integrity: Cannot be subclassed outside the package.
When a collection is constructed without any elements, a static empty map is returned.
I hope you try out some of google guavas immutable collections as they really improve the confidence of your codebase. When things cannot change it is good! Less bugs!
Don’t forget to subscribe for more great videos!
Happy coding!
Philip
Links:
Google Guava Immutable Collections Explained
https://github.com/google/guava/wiki/...
Google Guava ImmutableCollection JavaDoc
https://google.github.io/guava/releas...
Nesta página do site você pode assistir ao vídeo on-line Google Guava Immutable Collections Example duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Philip Starritt 20 Setembro 2017, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 2,700 vezes e gostou 30 espectadores. Boa visualização!