1、泛型
1.1、需求引出
package generic;
import java.util.ArrayList;
@SuppressWarnings({"all"})
public class Generic01 {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(new Dog("旺财", 10));
arrayList.add(new Dog("发财", 1));
arrayList.add(new Dog("小黄", 5));
arrayList.add(new Cat("招财猫", 8));
for (Object o : arrayList) {
Dog dog = (Dog) o;
System.out.println(dog.getName() + " - " + dog.getAge());
}
}
}
class Dog {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Cat {
private String name;
private int age;
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
1.2、使用传统方法的问题分析
- 不能对加入到集合 ArrayList中的数据类型进行约束(不安全)
- 遍历的时候,需要进行类型转换,如果集合中的数据量较大,对效率有影响
1.3、用泛型来解决前面的问题
ArrayList arrayList = new ArrayList();
package generic;
import java.util.ArrayList;
@SuppressWarnings({"all"})
public class Generic01 {
public static void main(String[] args) {
ArrayList<Dog> arrayList = new ArrayList<Dog>();
arrayList.add(new Dog("旺财", 10));
arrayList.add(new Dog("发财", 1));
arrayList.add(new Dog("小黄", 5));
for (Dog dog : arrayList) {
System.out.println(dog.getName() + " - " + dog.getAge());
}
}
}
class Dog {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Cat {
private String name;
private int age;
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
1.4、泛型的好处
1.5、泛型介绍
package generic;
public class Generic03 {
public static void main(String[] args) {
Person<String> person = new Person<String>("hello");
person.show();
Person<Integer> person2 = new Person<>(100);
person2.show();
}
}
class Person<E> {
E s;
public Person(E s) {
this.s = s;
}
public E f() {
return s;
}
public void show() {
System.out.println(s.getClass());
}
}
1.6、泛型的语法
1.6.1、泛型的声明
1.6.2、泛型的实例化
1.6.3、泛型使用举例
package generic;
import java.util.*;
public class GenericExercise {
public static void main(String[] args) {
HashSet<Student> students = new HashSet<Student>();
students.add(new Student("jack", 18));
students.add(new Student("tom", 28));
students.add(new Student("mary", 19));
for (Student student : students) {
System.out.println(student);
}
HashMap<String, Student> hm = new HashMap<String, Student>();
hm.put("milan", new Student("milan", 38));
hm.put("smith", new Student("smith", 48));
hm.put("jack", new Student("jack", 15));
Set<Map.Entry<String, Student>> entries = hm.entrySet();
Iterator<Map.Entry<String, Student>> iterator = entries.iterator();
System.out.println("==============================");
while (iterator.hasNext()) {
Map.Entry<String, Student> next = iterator.next();
System.out.println(next.getKey() + "-" + next.getValue());
}
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
|