当前位置:网站首页>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
边栏推荐
- 3W word will help you master the C language as soon as you get started - the latest update is up to 5.22
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
- Entitas learning [3] multi context system
- Reptile learning 4 winter vacation learning series (1)
- Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology
- Number and math classes
- Entitas learning [iv] other common knowledge points
- C language memory layout
- QQ get group information
- R built in data set
猜你喜欢
Serialization oriented - pickle library, JSON Library
Properties and methods of OS Library
DVC use case (VI): Data Registry
Day01 preliminary packet capture
(August 10, 2021) web crawler learning - Chinese University ranking directed crawler
2020 Summary - Magic year, magic me
How to create a new virtual machine
(August 9, 2021) example exercise of air quality index calculation (I)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
Automatic translation between Chinese and English
随机推荐
re. Sub() usage
20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
QQ get group link, QR code
03_ Armv8 instruction set introduction load and store instructions
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
Simple understanding of generics
Cacti主机模板之定制版
Introduction to Lichuang EDA
Entitas learning [3] multi context system
DVC use case (VI): Data Registry
Shift EC20 mode and switch
Ternsort model integration summary
Object. Assign () & JS (= >) arrow function & foreach () function
thread
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
Exceptions and exception handling
Heartbeat报错 attempted replay attack
Global function Encyclopedia
Reptile learning 3 (winter vacation learning)
Simple understanding of string