当前位置:网站首页>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
边栏推荐
- Using terminal connection in different modes of virtual machine
- Some summaries of the 21st postgraduate entrance examination 823 of network security major of Shanghai Jiaotong University and ideas on how to prepare for the 22nd postgraduate entrance examination pr
- Recommend a cool geospatial data visualization tool with low code
- World document to picture
- 本地Mysql忘记密码的修改方法(windows)[通俗易懂]
- Experiment 7. IPv6
- SSH principle and public key authentication
- Application of slice
- (August 9, 2021) example exercise of air quality index calculation (I)
- Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
猜你喜欢

Summary of Shanghai Jiaotong University postgraduate entrance examination module -- cryptography

Digital simulation beauty match preparation -matlab basic operation No. 6
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5](/img/68/4f92ca7cbdb90a919711b86d401302.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5

20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13

Attributes and methods in math library

Here, the DDS tutorial you want | first experience of fastdds - source code compilation & Installation & Testing

How to create a new virtual machine

Take advantage of the world's sleeping gap to improve and surpass yourself -- get up early

Notes on writing test points in mind mapping
![Entitas learning [3] multi context system](/img/f9/a3ce86ff2121dd1043305b7e834cc5.jpg)
Entitas learning [3] multi context system
随机推荐
[Android reverse] function interception instance (③ refresh CPU cache | ④ process interception function | ⑤ return specific results)
Recommend a cool geospatial data visualization tool with low code
Reptile learning 4 winter vacation learning series (1)
QQ group collection
LxC shared directory addition and deletion
Shift EC20 mode and switch
Postman advanced
Object. Assign () & JS (= >) arrow function & foreach () function
Review of week 278 of leetcode II
Force buckle 142 Circular linked list II
Four sorts: bubble, select, insert, count
VPS安装Virtualmin面板
Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
Customized version of cacti host template
JD home programmers delete databases and run away. Talk about binlog, the killer of MySQL data backup
Games101 Lesson 8 shading 2 Notes
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
C language memory layout
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
Summary of Shanghai Jiaotong University postgraduate entrance examination module -- cryptography