1. 泛型
加入泛型后,我们存放在集合中的数据类型将被限制在泛型内
编译器会对泛型进行识别,当我们添加的元素类型不符合时,会报错
遍历时,减少了类型转换的次数,可以直接以泛型的类型遍历
1.泛型又称参数化类型
2.泛型可以保证如果程序在编译时没有发出警告,运行时就不会产生ClassCastException异常
3.泛型的作用是:可以在类声明时通过一个标识表示类中属性的类型,或者是某个方法的返回值的类型,或者是参数类型。
4.泛型可以使用在接口和类,只能是引用类型
5.指定具体类型后,可以传入指定类型或者他的子类
6.当引用类型的泛型指定后,对象的泛型没有指定时,编译器会进行类型推断,自动认为对象的运行类型是指定的
7.当不指定泛型时,等价于指定的泛型是Object
public class Demo01 {
public static void main(String[] args) {
Person<String> person = new Person<>("n1c4n");
}
}
class Person<E>{
E s;//E表示 s的数据类型,该数据类型在定义Person对象的时候指定,即在编译期间,就确定E是什么类型。
public Person(E s){//E也可以是参数类型
this.s = s;
}
public E f(){//返回类型是E
return s;
}
}
public class Demo02 {
}
/*
1.Dog定义了泛型,但是没有指定泛型的数据类型,就称为自定义泛型类
2.T,B,C是泛型的标识符,一般是大写的单个字母
3.泛型标识符可以有多个
4.普通成员可以使用泛型,属性,方法,返回类型
5.泛型数组不能初始化,数组再创建时,会进行数据检查,不能确定泛型的类型,就不能在内存开空间
6.静态属性或方法不能使用泛型,加载类时,会加载静态修饰符,这时泛型的类型还不确定,所以JVM无法完成加载。
7.没有声明的泛型,不能直接使用
*/
class Dog<T,B,C>{
String name;
T t;
B b;
C c;
public Dog(String name, T t, B b, C c) {
this.name = name;
this.t = t;
this.b = b;
this.c = c;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
public C getC() {
return c;
}
public void setC(C c) {
this.c = c;
}
}
2.例子
- 1.创建 3 个学生对象
2.将学生对象放入到HashSet中
3.放入到HashMap中,要求Key是String name, Value 就是学生对象
import java.util.*;
public class Test01 {
public static void main(String[] args) {
HashSet<Student> students = new HashSet<>();
students.add(new Student("老米",24));
students.add(new Student("小超",20));
students.add(new Student("孙哥",23));
students.add(new Student("白姚",22));
for (Student student:
students) {
System.out.println(student);
}
HashMap<String,Student> hashMap = new HashMap<String,Student>();
hashMap.put("老米",new Student("老米",24));
hashMap.put("小超",new Student("小超",20));
hashMap.put("孙哥",new Student("孙哥",23));
hashMap.put("白姚",new Student("白姚",22));
Set<Map.Entry<String,Student>> entries = hashMap.entrySet();
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());
}
}
}
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 +
'}';
}
}
- 定义Employee类
1.该类包含:private成员变量name,sal,birthday,其中birthday为MyDate类的对象;
2.为每一个属性定义getter,setter方法
3.重写toString方法输出name,sal,birthday
4.MyDate包含:private成员变量 month,day,yea;
5.创建该类的3个对象,并放入ArrayList集合中,对集合的元素进行排序,并遍历输出;
排序的方式用sort并传入Comparator对象,按照name排序,name相同按照生日排序
import java.util.ArrayList;
import java.util.Comparator;
public class Test02 {
public static void main(String[] args) {
ArrayList<Employee> employees = new ArrayList<>();
employees.add(new Employee("Tom",800,new Employee.MyDate(1999,6,9)));
employees.add(new Employee("Jack",1800,new Employee.MyDate(2000,8,9)));
employees.add(new Employee("Lucy",2800,new Employee.MyDate(1998,7,22)));
employees.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
int i = o1.getName().compareTo(o2.getName());
if (o1 == null || o2 == null) {
return 0;
}
if (i != 0) {
return i;
}
int y = o1.getBirthday().getYear() - o2.getBirthday().getYear();
if (y!=0){
return y;
}
int m = o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
if (m!=0){
return m;
}
int d = o1.getBirthday().getDay() - o2.getBirthday().getDay();
return d;
}
});
System.out.println(employees);
}
}
class Employee{
private String name;
private int salary;
private MyDate birthday;
public Employee(String name, int salary, MyDate birthday) {
this.name = name;
this.salary = salary;
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
", birthday=" + birthday +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public static class MyDate {
private int year;
private int month;
private int day;
@Override
public String toString() {
return year + "年" + month + "月" + day + "日";
}
public int getYear() {
return year;
}
public void setYear(int year) {
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 MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
}
- 定义个泛型类DAO,在其中定义一个Map成员变量,Map的键为String类型,值为T类型。
创建几个方法:
1.public void save(String id , T entity):保存T类型的对象到Map成员变量中
2.public T get(String id):从map中获取id对应的对象
3.public void update(String id,T entity):替换map中key为id的内容,改为entity对象
4.public List list():返回map中存放的所有T对象
5.public void delete(String id):删除指定id对象
定义一个User类:
该类包含:private成员变量(int类型)id,age;(String)name。
创建DAL类的对象,分别调用save,get,update,list,delete方法来操作User对象
使用Junit单元测试类来进行测试
}
public void update(String id,T entity){
map.replace(id,entity);
}
public List<T> list(){
ArrayList list = new ArrayList<>();
for (Object value:
map.values()) {
list.add( value);
}
return list;
}
public void delete(String id){
map.remove(id);
}
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", age=" + age +
", 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;
}
}
3. 这段时间的总结
这几天我明显感觉自己的学习效率下降了,我仔细的思考了这个问题,我觉得主要有两个方面的原因。
- 1.学习的内容不断增加,前面的学习内容,需要时间来消化吸收,不断的去追源码,去看底层,其实本身是很累的,加上在集合这部分内容,底层的东西是混在一起的,比如HashSet是Set接口的实现类,但它的底层实际上联系到Map接口下的HashMap,这使得我的大脑总是在将他们混乱,再理清,在混乱。
- 2.刚开始学习,在缺乏完整的对程序结构的认识的情况下,理解本身就受到限制,只有先完整的对程序有一个了解,再不断的去完善对部分的理解,才能真正深入理解每个部分在程序中的作用,扮演的角色。
基于这两点,我认为我现在应当先快速完善知识体系,对知识中的细节,难点,通过问题去引导,解决问题本身就是一种优秀的学习方式,我应该快速的将数据库,开发框架,javaweb等知识学习一遍后,开始自己做项目的过程,过程中遇到的问题,分别去解决。这也是接下来的学习的想法,我将结束java基础的学习,开始数据库的学习,并且开始做leetbook的题目,来熟悉基础部分。
如此高密度的知识,本身如果有足够的时间,是应该完整的,细致的去理解学习,才能帮助后面的学习的,但我的目的是转行,并且我即将脱产,我并非科班,有足够的时间去深入的攻克难点,我认为基础部分的学习绝不是只有一遍的,在完成整个体系的快速学习后,我会再进行基础的巩固。