当前位置:网站首页>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();
}
}边栏推荐
- Go deep into the working principle of browser and JS engine (V8 engine as an example)
- Five simple data types of JS
- Go Context - Cancelation and Propagation
- 电子协会 C语言 1级 28 、字符菱形
- About the definition of pointer variables (personal notes)
- C language - minesweeping
- Customize the console plot result style
- Ctfhub eggs
- Basic bit operation symbols of C language
- Word of the Day
猜你喜欢

Deep analysis of recursion in quick sorting

Incorrect dependency of POM file
![H5 native player [learn video]](/img/51/83a200d0423b7274d1e981ec2ede2c.jpg)
H5 native player [learn video]

Depth of binary tree

The construction and usage of wampserver framework

Create an environment for new projects

Small sample learning data set

Detailed summary of position positioning

C language - minesweeping

渗透测试-提权专题
随机推荐
Detailed summary of flex layout
2022.1.23 diary
February 19 CTF exercise
How to add an external header file in vs?
Essais de pénétration - sujets d'autorisation
Critical dependency: require function is used in a way in which dependencies
ERDAS 9.2 installation tutorial
Can bus extended frame
Array and simple function encapsulation cases
Understand JS high-order function and write a high-order function
Conflict between v-mode and v-decorator in Ant Design
Go Concurrency
Using JS to realize the sidebar of life information network
Even if you are not good at anything, you are growing a little bit [to your 2021 summary]
Attack and defense world web baby Web
滲透測試-提權專題
The article is on the list. Welcome to learn
2.20 learning content
MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code
Use serialize in egg to read and write split tables