当前位置:网站首页>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
边栏推荐
- Solaris 10网络服务
- Iframe to only show a certain part of the page
- QQ group collection
- Fundamentals of software testing
- Supercomputing simulation research has determined a safe and effective carbon capture and storage route
- Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
- Recommend a cool geospatial data visualization tool with low code
- Some tips on learning database
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
- (2021-08-20) web crawler learning 2
猜你喜欢
Decrypt the advantages of low code and unlock efficient application development
QQ group administrators
Reptile learning 3 (winter vacation learning)
Realize cross tenant Vnet connection through azure virtual Wan
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
Review of week 278 of leetcode II
Climb Phoenix Mountain on December 19, 2021
Reptile learning winter vacation series (2)
Entitas learning [iv] other common knowledge points
How to judge the advantages and disadvantages of low code products in the market?
随机推荐
本地Mysql忘记密码的修改方法(windows)
Experiment 7. IPv6
Common built-in modules
Number and math classes
C language memory layout
3W word will help you master the C language as soon as you get started - the latest update is up to 5.22
Xiaobing · beauty appraisal
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10
Review of week 278 of leetcode II
Is Sanli futures safe? How to open a futures account? How to reduce the handling charge of futures at present?
Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology
Reptile learning 4 winter vacation learning series (1)
OSI model notes
thread
Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
Entitas learning [3] multi context system
Games101 Lesson 8 shading 2 Notes
Simple understanding of generics
Post man JSON script version conversion
Test question bank management system - database design [easy to understand]