Guava study notes: The meaning of immutable sets of guava
Immutable objects have many advantages, including:
- When an object is called by an untrusted library, the immutable form is secure;
- When an immutable object is called by multiple threads, there is no race condition problem
- Immutable collections do not need to consider changes, so you can save time and space. All immutable collections have better memory utilization (analysis and testing details) than their mutable form;
- Immutable objects can be used safely as constants because they are fixed.
Creating an immutable copy of an object is a good defensive programming technique. Guava provides an easy-to-use immutable version for all JDK standard collection types and guava new collection types.
The JDK also provides the Collections.unmodifiablexxx method to wrap the collection in an immutable form, but we think it is not good enough:
- Cumbersome and cumbersome: it is not comfortable to use in all scenarios where defensive copies are to be made;
- Unsafe: To ensure that no one is modified by reference to the original set, the returned collection is virtually immutable;
- Inefficient: The wrapped collection still retains the overhead of a mutable collection, such as checking for concurrent modifications, extra space for the hash table, and so on.
If you do not modify the requirements of a collection, or if you want a collection to remain unchanged, it is a good practice to copy it defensively to immutable collections.
Important: None of the implementations of the guava immutable collection accept null values. We did a detailed study of the code base in Google, and found that only 5% of the cases needed to allow null elements in the collection, and the remaining 95% scenarios were fast failing with null values. If you need to use NULL in the immutable collection, use the Collections.unmodifiablexxx method in the JDK. For more details, refer to "using and avoiding null".
How to use the immutable collection of guava 1. How to create immutable collections
The first method is created using builder:
Public Class Immutabledemo {Public Static voidMain(String[]Args) {Set<String>Immutablenamedcolors= Immutableset.<String>Builder().Add("Red", "Green","Black","White","Grey").Build (); //immutablenamedcolors.add ("abc"); for (string< Span class= "PLN" > color : Immutablenamedcolors) { System.. Printlncolor} } /span>
The second method is created using the of static method:
Immutableset. of("Red","Green","Black","White","Grey");
The third method is created using the copyof static method:
Immutableset. CopyOf(newString[]{"Red","Green","Black","White"," Grey "});
2. Use Aslist () to get a list view of immutable collections
The Aslist method is defined in Immutablecollection, and all immutable collections inherit from Immutablecollection, so all immutable collections will have a aslist () method that returns a list view of the current immutable collection. This view is also immutable.
3. Use of immutable collections
Immutable collections are used just like normal collections, except that they cannot be used to modify the collection's methods, such as their add,remove.
Guava Study notes: immutable collection of guava