当前位置:网站首页>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();
}
}边栏推荐
- Personalized Federated Learning with Moreau Envelopes
- The k-th node of the binary search tree [sword finger offer]
- 2.20 learning content
- Use serialize in egg to read and write split tables
- [relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear
- Attack and defense world web baby Web
- Two dimensional array and function call cases of C language
- Instant messaging project (I)
- Go Context - Cancelation and Propagation
- The construction and usage of wampserver framework
猜你喜欢

Use serialize in egg to read and write split tables

滲透測試-提權專題

Detailed summary of flex layout
![[day40 literature extensive reading] space and time in the child's mind: metallic or atomic](/img/98/10b3e63c9609990c51b619d9ca6179.jpg)
[day40 literature extensive reading] space and time in the child's mind: metallic or atomic

Deep analysis of epoll reactor code

Go implements LRU cache

Analysis of IM project framework

Monkey test of APP automation

Semantic segmentation cvpr2020 unsupervised intra domain adaptation for semantic segmentation through self supervision

Double recursion in deep analysis merge sort
随机推荐
Dynamic programming example 2 leetcode62 unique paths
SQL get current time
Makefile Foundation
JS function to realize simple calculator
Dynamic programming Backpack - 01 Backpack
CUDA compilation error
Detailed summary of float
[relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear
Monkey test of APP automation
Interface learning
Voxel based and second network learning
Semantic segmentation cvpr2020 unsupervised intra domain adaptation for semantic segmentation through self supervision
Use of MySQL variables
Enhanced paste quill editor
Mobile number regular expression input box loses focus verification
Read the general components of antd source code
Two dimensional array and function call cases of C language
Code learning-cvpr2020 unsupervised domain adaptive semantic segmentation: intra advance
Database overview
Oracle SQL statement operand: rounding, rounding, differentiation and formatting