Java自学笔记——泛型
普通集合和泛型区别
public class ArrayList<E> {}
,E称为泛型- E 具体的数据类型在定义对象时指定,即在编译期间,就确定 E 的数据类型
普通集合
- 不能对加入集合的类型进行约束
- 遍历时需要类型转换,数据量大时,效率低
- 假如不小心加入一只 Cat 会抛出类型转化异常
/**
* @author Lspero
* @version 1.0
*/
public class Generic01 {
public static void main(String[] args) {
//1.不能对加入集合的类型进行约束
//2.遍历时需要类型转换,数据量大时,效率低
ArrayList arrayList = new ArrayList();
arrayList.add(new Dog("旺财", 10));
arrayList.add(new Dog("发财", 1));
arrayList.add(new Dog("小黄", 5));
//假如不小心加入一只Cat怎么办
arrayList.add(new Cat("招财", 8));//抛出类型转化异常
for (Object o :arrayList) {
Dog dog = (Dog) o;
System.out.println(dog.getName() + dog.getAge());
}
}
}
泛型
- 如果编译器添加的类型不满足要求会报错,不再提示编译警告
- 减少类型转化次数,提高效率
/**
* @author Lspero
* @version 1.0
*/
@SuppressWarnings("all")
public class Generic02 {
public static void main(String[] args) {
//使用泛型
//ArrayList<Dog>() 表示集合中存放的是Dog类型
//如果编译器添加的类型不满足要求会报错,不再提示编译警告
ArrayList<Dog> arrayList = new ArrayList<>();
arrayList.add(new Dog("旺财", 10));
arrayList.add(new Dog("发财", 1));
arrayList.add(new Dog("小黄", 5));
//假如不小心加入一只Cat,会报错
//arrayList.add(new Cat("招财",5));
for (Dog dog :arrayList) {
//减少类型转化次数,提高效率
System.out.println(dog.getName() + dog.getAge());
}
/*
*/
}
}
区别
不使用泛型,Dog对象 存入 ArrayList 会先转换成 Object 取出时还要转回 Dog
Dog -> Object 加入 -> 取出 Object -> Dog
使用泛型,存入和取出时不需要进行类型转化Dog -> Dog -> Dog
类方法属性使用泛型
定义Person类
//多种泛型 class<K, V, M ...> interface 接口<T> K,V,M,T 代表类型
class Person<E>{
//E 可以是属性类型
E s;// E 表示S的数据类型,数据类型在创建对象时指定
//E 可以是参数类型
public Person(E s) {
this.s = s;
}
//E 可以是返回类型
public E f(){
return s;
}
public void show(){
//显示 s 的运行类型
System.out.println(s.getClass());
}
}
创建对象时指定数据类型
/**
* @author Lspero
* @version 1.0
*/
@SuppressWarnings("all")
public class Generic03 {
public static void main(String[] args) {
//泛(广泛)型(类型),表示数据类型的数据类型
//又称参数化类型
//public class ArrayList<E> {},E称为泛型
//E 具体的数据类型在定义Person对象时指定,即在编译期间,就确定 E 的数据类型
Person<String> lspero = new Person<>("lspero");
lspero.show();
/*
上面的Person类相当于
class Person{
//E 可以是属性类型
String s;// E 表示S的数据类型,数据类型在创建对象时指定
//E 可以是参数类型
public Person(String s) {
this.s = s;
}
//E 可以是返回类型
public String f(){
return s;
}
public void show(){
//显示 s 的运行类型
System.out.println(s.getClass());
}
}
*/
}
}
HashSet泛型源码
/**
* @author Lspero
* @version 1.0
*/
@SuppressWarnings("all")
public class GenericExercise {
public static void main(String[] args) {
//使用泛型给hahaSet放入学生对象
HashSet<Student> students = new HashSet<Student>();
students.add(new Student("tom", 18));
students.add(new Student("jack", 28));
students.add(new Student("mary", 19));
for (Student student :students) {
System.out.println(student);
}
//使用泛型给hahaMap放入学生对象
//public class HashMap<K, V> {}
// K-String V-Student
HashMap<String, Student> hm = new HashMap<>();
hm.put("milan", new Student("milan", 50));
hm.put("smith", new Student("smith", 32));
hm.put("lspero", new Student("lspero", 47));
/*
public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es;
return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
}
*/
Set<Map.Entry<String, Student>> entries = hm.entrySet();
/*
public final Iterator<Map.Entry<K,V>> iterator() {
return new EntryIterator();
}
*/
Iterator<Map.Entry<String, Student>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, Student> next = iterator.next();
System.out.println(next.getKey() + " " + next.getValue());
}
//默认泛型为 Object
ArrayList arrayList = new ArrayList();//等价 ArrayList<Object> arrayList = new ArrayList<Object>();
arrayList.add(12);
/*
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
*/
Tiger tiger = new Tiger();
/*
class Tiger{
Object e;
public Tiger(){}
public Tiger(Object e) {
this.e = e;
}
}
*/
}
}