当前位置:网站首页>What is generics and the use of generics in collections [easy to understand]
What is generics and the use of generics in collections [easy to understand]
2022-06-25 18:05:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
What is generics ?
Generics are most commonly used with collections , Because generics were first added Java It is to solve the problem of set downward transformation . If we have such a need : Define a description circle , The data type in the circle is required to be indeterminate , That is, when the name attribute is used , Attribute types are indeterminate . For example, describe that there is a radius in a quasi circle , The required radius can be int, It can also be used. double. Then the data type is uncertain , Just use generics , Parameterize data types .
Use of generics in collections
List Use generics
When we create a collection, we use <> To declare List Collections can only be saved Dog Class object List dogs=new ArrayList<>(); establish Dog Class object Dog dog1 = new Dog(“101”,“ Laifu ”); add to Dog Object to collection dogs.add(dog1);// here dogs Only... Can be stored in the collection Dog Class object
public class ListTest {
public static void main(String[] args) {
// Statement List Collections can only be saved Dog Class object
List<Dog> dogs=new ArrayList<>();
// establish Dog object .
Dog dog1=new Dog();
dog1.setId(111);
dog1.setName(" Laifu ");
Dog dog2=new Dog();
dog2.setId(112);
dog2.setName(" Laifu 2");
//Dog Objects adding to collection
dogs.add(dog1);
dogs.add(dog2);
// Get an element
Dog dog = dogs.get(1);
System.out.println(dog);
// get List Iterators of sets
Iterator<Dog> it = dogs.iterator();
while(it.hasNext()) {
Dog next = it.next();
System.out.println(next);
}
// Use advanced for Traversal
for(Dog d:dogs) {
System.out.println("--"+d);
}
}
}Map Use generics
Create a collection object ,key by String type ,value by Dog Class types . Map<String,Dog> dogs=new HashMap<>(); take dog Object to save to Map Collection dogs.put(“111”, dog1);// At this time key Only string type ,value Can only be Dog type
summary :
The purpose of using generics in collections is to solve the problem of downward transformation , After the generic materialization , Collections can only store types that are materialized with generics .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/151141.html Link to the original text :https://javaforall.cn
边栏推荐
- Which of the top ten securities companies has the lowest commission? Is it safe to open an account
- 深度学习网路模型
- Sentinel sentinel mechanism
- bert之我的小总结
- 配电室环境的分布式远程管理
- Recursion and divide and conquer
- 视频制作素材网站整理
- 使用DiskGenius拓展系統盤C盤的容量
- 解决nvprof 报错ERR_NVGPUCTRPERM - The user does not have permission to profile on the target device.
- Unity technical manual - interference / noise sub module
猜你喜欢

观察者模式之通用消息发布与订阅

为什么在变频器场合需要安科瑞的电力有源滤波器?

SDN system method | 10 The future of SDN

解决nvprof 报错ERR_NVGPUCTRPERM - The user does not have permission to profile on the target device.

1、对范数的理解
沁恒CH583 USB 自定义HID调试记录

Deep understanding of ELF files

SDN系统方法 | 10. SDN的未来

Solve nvprof error err_ NVGPUCTRPERM - The user does not have permission to profile on the target device.

什么是算子?
随机推荐
CONDA modifying a mirror source
股票开户找人办比较方便么?在线开户安全么?
利用Qt制作美化登录界面框
[matlab] data interpolation
IET出席2022世界科技社团发展与治理论坛 为构建国际科技共同体献言献策
Swagger实现后台接口自动化文档
HMS core machine learning service realizes simultaneous interpretation, supports Chinese-English translation and multiple voice broadcast
[matlab] curve fitting
【 NLP 】 in this year's English college entrance examination, CMU delivered 134 high scores with reconstruction pre training, significantly surpassing gpt3
华为云GaussDB(for Redis)揭秘第19期:GaussDB(for Redis)全面对比Codis
The stacks 2022:32 marketing technology stacks selected
IVX 启航
CGI connects to database through ODBC
Are the top ten leading securities companies safe to open accounts
[compilation principle] lexical analysis
什么是算子?
Time series analysis of data mining [easy to understand]
Recursion and divide and conquer
Li Kou daily question - day 27 -561 Array splitting I
Centos7 installing redis 7.0.2