Google Guava Immutable Collections Example

Published: 20 September 2017
on channel: Philip Starritt
2,700
30

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...


On this page of the site you can watch the video online Google Guava Immutable Collections Example with a duration of hours minute second in good quality, which was uploaded by the user Philip Starritt 20 September 2017, share the link with friends and acquaintances, this video has already been watched 2,700 times on youtube and it was liked by 30 viewers. Enjoy your viewing!