当前位置:网站首页>Guava immutable set
Guava immutable set
2022-06-25 05:37:00 【Which floor do you rate moto】
Creating an immutable copy of an object is always a good defensive skill .
Guava For all the JDK Standard set types and Guava New collection types are available in an easy-to-use, immutable version
Advantages of immutable objects
When an object is called by an untrusted library , Immutable forms are safe
When an immutable object is called by multiple threads , No race condition problem
Immutable sets do not need to consider changes , So it can save time and space
Immutable objects have fixed variables , Can be used safely as a constant
JDK Provided unmodifiableXXX Method
Heavy and cumbersome
unsafe
Inefficient
Three ways to create immutable sets
copyOf Method :ImmutableSet.copyOf(set)
of Method :ImmutableSet.of("a","b","c")
Builder Tools :ImmutableSet:builder().build()
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
// use JDK Method will list Become immutable sets : This method is too cumbersome
List<Integer> newList = Collections.unmodifiableList(list);
// If you modify an immutable set, you will :Exception in thread "main" java.lang.UnsupportedOperationException
test(newList);
}
public class UnmodifiableTest {
public static void test(List<Integer> list){
list.remove(0);
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
// use JDK Method will list Become immutable sets : This method is too cumbersome
List<Integer> newList = Collections.unmodifiableList(list);
// If you modify an immutable set, you will :Exception in thread "main" java.lang.UnsupportedOperationException
test(newList);
}
public void unmodifiableList(){
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
// The first way to create immutable sets : Create from an existing collection
ImmutableSet<Integer> integers1 = ImmutableSet.copyOf(list);
// The second way to create immutable sets : Through the initial value , Create immutable collections
ImmutableSet<Integer> integers2 = ImmutableSet.of(1,2,3);
// The third way to create immutable sets : adopt builder establish
ImmutableSet<Object> integers3 = ImmutableSet.builder().add(1).addAll(Sets.newHashSet(1, 2, 3)).build();
}
}边栏推荐
- Basic knowledge of web pages (URL related)
- Critical dependency: require function is used in a way in which dependencies
- [Huawei machine test] hj16 shopping list
- Create dynamic array
- Dynamic programming full backpack
- Go Basics
- Semantic segmentation fcns in the wild: pixel level adaptive and constraint based adaptation
- cuda编译报错
- Duplicate symbols for architecture i386 clang
- About the definition of pointer variables (personal notes)
猜你喜欢

"APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare
![Bind simulation, key points of interpreting bind handwritten code [details]](/img/03/6aa300bb8b8342199aed5a819f3634.jpg)
Bind simulation, key points of interpreting bind handwritten code [details]

Word quickly makes multiple single-sided table labels, number plates, etc
![[OSPF routing calculation (class I LSA router, class II LSA network, and class III LSA sum net)] -20211228-30](/img/c7/e38a7a5cd4c1e4105c3f3f246276c6.jpg)
[OSPF routing calculation (class I LSA router, class II LSA network, and class III LSA sum net)] -20211228-30

Deep analysis of recursion in quick sorting

1.5.3 use tcpdump to observe ARP communication process

A review of small sample learning

Double recursion in deep analysis merge sort
![H5 canvas drawing circle drawing fillet [detailed explanation]](/img/6f/a33a323b6cd0918066e8b71a22d841.jpg)
H5 canvas drawing circle drawing fillet [detailed explanation]

JSON Library Tutorial from scratch (I): starting to learn and organize notes
随机推荐
By inserting a section break, the word header, footer, and page number can start from any page
Dynamic programming full backpack
Mobile number regular expression input box loses focus verification
Read the general components of antd source code
How to add an external header file in vs?
MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code
Word quickly makes multiple single-sided table labels, number plates, etc
SQL get current time
Reading and writing of nodejs excel (.Xlsx) files
Deep analysis of recursion in quick sorting
Use js to simply implement the apply, call and bind methods
On Transform
The construction and usage of wampserver framework
2.20 learning content
Even if you are not good at anything, you are growing a little bit [to your 2021 summary]
Matlab notes
ERDAS 9.2 installation tutorial
Mirror image of binary tree
Deeply understand the characteristics of standard flow and off standard elements
2021-03-23