当前位置:网站首页>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
边栏推荐
- 图标丢失,URL附带JESSSIONID的什么来的?
- 股票开户找人办比较方便么?在线开户安全么?
- Sentinel sentinel mechanism
- Garbage collector and memory allocation strategy
- Idea global search for Chinese characters [easy to understand]
- CONDA modifying a mirror source
- About queryinterface functions
- IVX 启航
- 存在重复元素III[利用排序后的有序性进行剪枝]
- Utilisation de diskgenius pour augmenter la capacité du disque système C
猜你喜欢

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

What is an operator?

使用DiskGenius拓展系统盘C盘的容量

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

New characteristics of cultural consumption in the era of digital economy

微博评论的计算架构

解析数仓lazyagg查询重写优化

移动端异构运算技术 - GPU OpenCL 编程(基础篇)

怎么判断自己是否适合转行软件测试

喜报|海泰方圆通过CMMI-3资质认证,研发能力获国际认可
随机推荐
十大证券公司哪个佣金最低 办理开户安全吗
SQL Server real time backup library requirements
实际开户复杂吗?在线开户安全么?
Article 7: there is no code prompt in clion,,,
MVDR beam MATLAB, MVDR beam forming matlab[easy to understand]
有关均衡----简易版瓶颈模型
Encryption trend: Fashion advances to the meta universe
What is an operator?
Accumulation of some common knowledge points
QT generate random numbers (random strings) within the specified range
HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
Agent white paper - jointly build agents and create the wisdom of the whole scene | cloud library No.21 recommendation
ACY100油烟浓度在线监控仪针对饮食业厨房油烟排放
Utilisation de diskgenius pour augmenter la capacité du disque système C
SQL Server实时备份库要求
Are the top ten leading securities companies safe to open accounts
How about qiniu's Zhangle TenPay? Is it safe
158_模型_Power BI 使用 DAX + SVG 打通制作商业图表几乎所有可能
什么是算子?
Unity technical manual - interference / noise sub module