当前位置:网站首页>Googgle guava ImmutableCollections
Googgle guava ImmutableCollections
2022-07-04 11:44:00 【Brother Wang_】
Googgle guava ImmutableCollections
Reference documents
Google Guava ImmutableCollections Official documents
Immutable objects have many advantages( Immutable classes have many benefits )
- Safe for use by untrusted libraries( Use untrusted libraries safely ), I remember Effective Java I saw , Because what is passed is a set or object , This will lead to many situations , The external may change the incoming object , Or the collection or object returned by the current class may also destroy the currently returned reference information , So protect , What you get and what you go out will not be destroyed by outsiders .
- Thread-safe: can be used by many threads with no risk of race conditions.( Thread safety : It can be used by many threads , There is no risk of competitive conditions ) Each thread gets a copy of an immutable class .
- Can be used as a constant , Expect it to remain fixed .
- Making immutable copies of objects is a good defense programming technique a good defensive programming technique
- When you don't want to modify the set , Or expect the set to remain unchanged , It is a good practice to copy it defensively into an immutable set .
How to create immutable classes
- copyOf()
public static void usingCopyOf(){
Set<Integer> set = new HashSet<Integer>();
set.add(1);
set.add(2);
ImmutableSet<Integer> immutableSet = ImmutableSet.copyOf(set);
// Adding elements is not supported here
/*immutableSet.add(4);
Set<Integer> addOne = new HashSet<>();
addOne.add(5);
immutableSet.addAll(addOne);*/
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- of()
of Methods are overloaded with many numbers , The last one is the optional extension parameter .
public static void usingOf(){
ImmutableSet<Integer> ofSet = ImmutableSet.of(1,2,3,4,5,6,7);
System.out.println(ofSet);
}
- 1.
- 2.
- 3.
- 4.
- Use builder mode to create
public static void usingBuilder(){
ImmutableSet<Integer> builderSet = ImmutableSet.<Integer>builder()
.add(1)
.add(2)
.build();
System.out.println(builderSet);
}
/**
* Returns a new builder. The generated builder is equivalent to the builder
* created by the {@link Builder} constructor.
*/
public static <E> Builder<E> builder() {
return new Builder<E>();
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- asList()
All immutable sets provide a ImmutableList View asList() Even if you have data stored as one ImmutableSortedSet, You can get k The smallest element sortedSet.asList().get(k).
Main set
Variable set type | Variable collection sources :JDK or Guava | Guava Immutable set |
Collection | JDK | ImmutableCollection |
List | JDK | ImmutableList |
Set | JDK | ImmutableSet |
SortedSet/NavigableSet | JDK | ImmutableSortedSet |
Map | JDK | ImmutableMap |
SortedMap | JDK | ImmutableSortedMap |
Multiset | Guava | ImmutableMultiset |
SortedMultiset | Guava | ImmutableSortedMultiset |
Multimap | Guava | ImmutableMultimap |
ListMultimap | Guava | ImmutableListMultimap |
SetMultimap | Guava | ImmutableSetMultimap |
BiMap | Guava | ImmutableBiMap |
ClassToInstanceMap | Guava | ImmutableClassToInstanceMap |
Table | Guava | ImmutableTable |
The next step is to understand these collections
边栏推荐
- TCP slicing and PSH understanding
- Login operation (for user name and password)
- Test question bank management system - database design [easy to understand]
- QQ get group settings
- Usage of with as
- Object. Assign () & JS (= >) arrow function & foreach () function
- Local MySQL forget password modification method (Windows) [easy to understand]
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
- Cacti主机模板之定制版
- [ES6] template string: `string`, a new symbol in es2015
猜你喜欢
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20](/img/d5/4bce239b522696b5312b1346336b5f.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20

Usage of case when then else end statement
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19](/img/7c/f728e88ca36524f92c56213370399b.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19

TCP slicing and PSH understanding

Post man JSON script version conversion
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14](/img/c5/dde92f887e8e73d7db869fcddc107f.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
![Entitas learning [3] multi context system](/img/f9/a3ce86ff2121dd1043305b7e834cc5.jpg)
Entitas learning [3] multi context system

Using terminal connection in different modes of virtual machine

Reptile learning 3 (winter vacation learning)

Serialization oriented - pickle library, JSON Library
随机推荐
SSH principle and public key authentication
Heartbeat启动后无反应
How to judge the advantages and disadvantages of low code products in the market?
Simple understanding of string
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
Login operation (for user name and password)
os. Path built-in module
Snowflake won the 2021 annual database
C language memory layout
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
(August 9, 2021) example exercise of air quality index calculation (I)
[Android reverse] function interception instance (③ refresh CPU cache | ④ process interception function | ⑤ return specific results)
Climb Phoenix Mountain on December 19, 2021
Dos and path
Oracle11g | getting started with database. It's enough to read this 10000 word analysis
Definition and method of string
OSI model notes
In 2022, financial products are not guaranteed?
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22