泛型
1.为什么需要泛型
1.不能对加入到集合ArrayList中的数据类型进行约束
2.遍历的时候,需要进行类型转换,如果集合中的数据量大,对效率有影响
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| ArrayList<Dog>l1 = new ArrayList<Dog>(); dog d1 = new dog ("dog1",18); dog d2 = new dog ("dog2",19); dog d3 = new dog ("dog3",20); l1.add(d1); l1.add(d2); l1.add(d3); for(Dog dog:l1) { System.out.println(dog.getname()+dog.getAge()); } class dog { String name; 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; } @Override public String toString() { return "dog [name=" + name + ", age=" + age + "]"; } }
|
2.泛型的好处
1.编译时,检查添加元素的类型,提高了安全性
2.减少了类型转换次数,提高效率
3.什么是泛型
1.广泛的类型就是泛型
2.泛型又称参数化类型,用来解决数据类型的安全性问题
3.在类声明或实例化时只要指定好需要的具体的类型即可
4.泛型的作用是:可以在类声明时通过一个标识表示类中某个属性的类型,或者是某个方法的返回值类型,或者是参数类型
4.泛型语法
1 2 3 4 5 6 7 8 9 10 11 12
| interface 接口 <T>{ }和 class 类<K,V>{ }
List<String> strList = new ArrayList<String>(); lterator<Customer> iterator = customers.iterator();
|
5.泛型应用实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
HashSet<stu> students = new HashSet<stu>(); students.add(new stu("haozi",18)); students.add(new stu("jiaozi",16)); for(stu s:students) { System.out.println(s); } HashMap<String,stu> hm = new HashMap<String,stu>(); hm.put("1", new stu("haozi",18)); hm.put("2", new stu("jiaozi",16)); Set<Map.Entry<String, stu>> entries = hm.entrySet(); Iterator<Map.Entry<String, stu>> iterator = entries.iterator(); while(iterator.hasNext()) { Map.Entry<String, stu> next = iterator.next(); System.out.println(next.getKey()+next.getValue()); } class stu{ String name; int age; public stu(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 "stu [name=" + name + ", age=" + age + "]"; } }
|
6.泛型细节
1.给泛型指向数据类型要求的是引用类型,不能是基本数据类型
2.再给泛型指定了具体类型后,可以传入该类型或者其子类的类型
3.可以简写泛型
1
| List<Integer> list1 = new ArrayList<>();
|
4.案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| import java.util.*; public class work13 { public static void main(String[]args) { ArrayList<Employee> al = new ArrayList<>(); al.add(new Employee("haozi2",18000,new MyDate(11,11,2000))); al.add(new Employee("haozi2",1800,new MyDate(12,11,2000))); al.add(new Employee("haozi3",180,new MyDate(10,10,1980))); System.out.println(al); al.sort(new Comparator<Employee>() { public int compare(Employee o1,Employee o2) { if(!(o1 instanceof Employee && o2 instanceof Employee)) { System.out.println("类型不正确"); } int i = o1.getName().compareTo(o2.getName()); if(i!=0) { return i; } int year = o1.getBirthday().getYear() - o2.getBirthday().getYear(); if(year !=0) { return year; } int month = o1.getBirthday().getMonth() - o2.getBirthday().getMonth(); if(month !=0) { return month; } return o1.getBirthday().getDay() - o2.getBirthday().getDay(); } }); System.out.println(); System.out.println(al); } } class Employee{ String name; int sal; MyDate birthday; public Employee(String name,int sal,MyDate birthday) { this.name=name; this.sal=sal; this.birthday=birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSal() { return sal; } public void setSal(int sal) { this.sal = sal; } public MyDate getBirthday() { return birthday; } public void setBirthday(MyDate birthday) { this.birthday = birthday; } @Override public String toString() { return "\n [name=" + name + ", sal=" + sal + ", birthday=" + birthday + "]"; } } class MyDate{ private int month; private int day; private int year; public MyDate(int month,int day,int year) { this.month=month; this.day=day; this.year=year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } @Override public String toString() { return "MyDate [month=" + month + ", day=" + day + ", year=" + year + "]"; } }
|
7.自定义泛型类
1.语法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| class 类名 <T,R...>{ 成员 }
class Tiger<T,R,M>{ String name; R r; M m; T t; T[] ts = new T[8]; static R r2; public Tiger(String name,R r,M m,T t){ this.name = name; this.r = r; this.m = m; this.t = t; } }
|
2.自定义泛型接口
1 2 3 4 5 6 7 8 9
| interface 接口名<T,R...>{
}
|
3.自定义泛型方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| 修饰符 <T,R...>返回类型 方法名(参数列表){
}
public static void main(String[]args){ cat c1 = new cat(); c1.fly("宝马",100); } class car{ public void run(){
}
public <T,R> void fly(T t,R r){
} }
|
4.泛型的继承和通配符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
public static void printCollection1(List<?> c){ for(Object object:c){ System.out.println(object); } }
public static void printCollection2(List<? extends AA> c){ for(Object object:c){ System.out.println(object); } }
public static void printCollection2(List<? super AA> c){ for(Object object:c){ System.out.println(object); } }
|
5.案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| import java.util.*; public class work1 { public static void main(String[]args) { DAO<User> d = new DAO<>(); d.save("1", new User(1,1000,"haozi1")); d.save("2", new User(2,2000,"haozi2")); d.save("3", new User(3,3000,"haozi3")); System.out.println(d.list()); d.update("3", new User(3,5000,"haozi3")); System.out.println(d.list()); d.delete("3"); System.out.println(d.list()); } } class User{ private int id; private int age; private String name; public User(int id,int age,String name) { this.id=id; this.age=age; this.name=name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", age=" + age + ", name=" + name + "]"; } } class DAO<T>{ private Map<String,T> map = new HashMap<>(); public void save(String id,T entity) { map.put(id, entity); } public T get(String id) { return map.get(id); } public void update(String id,T entity) { map.put(id, entity); } public List<T> list(){ List<T> list = new ArrayList<>(); Set<String> keyset = map.keySet(); for(String key:keyset) { list.add(map.get(key)); } return list; } public void delete(String id) { map.remove(id); } }
|