当前位置:网站首页>Use of generics
Use of generics
2022-07-03 07:21:00 【four thousand three hundred and forty-three】


package Generic;
import org.junit.Test;
import java.util.*;
public class GenericTest {
@Test
public void test1(){
// Cases where generics are not used :
ArrayList list = new ArrayList();
// demand : Store student scores
list.add(89);
list.add(87);
list.add(86);
list.add(90);
// problem 1: The type is not safe
// list.add("tom");
for (Object obj : list){
// problem 2: Type conversion exceptions may occur during strong conversion
int stuScore = (int)obj;
System.out.println(stuScore);
}
}
// Using generics in collections
// Generic is a class , Cannot be a basic data type
@Test
public void test2(){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(78);
list.add(79);
list.add(80);
list.add(77);
// Type checking occurs at compile time , Ensure data security
// list.add("Tom");
int stuScore;
for(Integer score: list){
// The forced rotation operation is avoided
stuScore = score;
System.out.println(stuScore);
}
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()){
stuScore = iterator.next();
System.out.println(stuScore);
}
}
// Using generics in collections :HashMap
@Test
public void test3(){
Map<String ,Integer> map = new HashMap<String, Integer>();
map.put("Laowang",40);
map.put("Xiaodu",10);
map.put("Xiaoli",3);
map.put("Laohei",95);
// The nesting of generics appears
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();
while (iterator.hasNext()){
Map.Entry<String, Integer> entry = iterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}
边栏推荐
- Use of framework
- Raspberry pie update tool chain
- dataworks自定义函数开发环境搭建
- Specified interval inversion in the linked list
- [plus de détails] dernière entrevue complète redis (50)
- PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
- Inno Setup 制作安装包
- Advanced API (multithreading 02)
- Advanced API (batch image Download & socket dialog)
- dataworks自定義函數開發環境搭建
猜你喜欢

JS monitors empty objects and empty references

File operation serialization recursive copy

My 2020 summary "don't love the past, indulge in moving forward"

Common problems in io streams

在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储

SecureCRT password to cancel session recording

Recursion, Fibonacci sequence

Basic components and intermediate components

TCP cumulative acknowledgement and window value update

Use the jvisualvm tool ----- tocmat to start JMX monitoring
随机推荐
"Moss ma not found" solution
Final, override, polymorphism, abstraction, interface
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
LeetCode
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
Mise en place d'un environnement de développement de fonctions personnalisées
sharepoint 2007 versions
High concurrency memory pool
7.2 brush two questions
4279. Cartesian tree
Recursion, Fibonacci sequence
TypeScript let與var的區別
【最詳細】最新最全Redis面試大全(50道)
JMeter JSON extractor extracts two parameters at the same time
[solved] sqlexception: invalid value for getint() - 'Tian Peng‘
专题 | 同步 异步
Liang Ning: 30 lectures on brain map notes for growth thinking
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
JS monitors empty objects and empty references
TypeScript let与var的区别