当前位置:网站首页>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
边栏推荐
- [matlab] data interpolation
- Sentinel sentinel mechanism
- conda安装的py3.6和py3.7
- IET出席2022世界科技社团发展与治理论坛 为构建国际科技共同体献言献策
- 延时函数如何延时
- 使用DiskGenius拓展系统盘C盘的容量
- Operating steps for installing CUDA in win10 (continuous improvement)
- 微博评论的计算架构
- Is it convenient to open a stock account? Is online account opening safe?
- Which of the top ten securities companies has the lowest commission? Is it safe to open an account
猜你喜欢

Optimization of lazyagg query rewriting in parsing data warehouse

SDN系统方法 | 9. 接入网
![[compilation principle] overview](/img/af/246750beb72842e83a19e42270c09e.png)
[compilation principle] overview

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

解析数仓lazyagg查询重写优化

篇4:win10安装MingW64

Garbage collector and memory allocation strategy
沁恒CH583 USB 自定义HID调试记录

158_模型_Power BI 使用 DAX + SVG 打通制作商业图表几乎所有可能

观察者模式之通用消息发布与订阅
随机推荐
【 NLP 】 in this year's English college entrance examination, CMU delivered 134 high scores with reconstruction pre training, significantly surpassing gpt3
lock
Hover effect of eight buttons
QT using SQLite database
[compilation principle] lexical analysis
conda安装的py3.6和py3.7
力扣每日一题-第27天-561.数组拆分Ⅰ
Vscode automatically generates ifndef define ENDIF of header file
Android物联网应用程序开发(智慧园区)—— 图片预览界面
Using QT to make a beautiful login interface box
Operating steps for installing CUDA in win10 (continuous improvement)
What is an operator?
解决nvprof 报错ERR_NVGPUCTRPERM - The user does not have permission to profile on the target device.
Swagger implements background interface automation document
智能体白皮书 - 共建智能体,共创全场景智慧 | 云享书库NO.21推荐
ASP. Net supermarket convenience store online shopping mall source code, for the surrounding distribution system
Garbage collector and memory allocation strategy
The stacks 2022:32 marketing technology stacks selected
Use of jupyter
哈希竞猜游戏系统开发如何开发?哈希竞猜游戏系统开发应用详情案例及源码