泛型
练习一
package com.tk.generic;
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = 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;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
}
package com.tk.generic;
public class Employee {
private String name;
private double sal;
private MyDate birthday;
public Employee(String name, double 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 double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "\nEmployee{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
}
package com.tk.generic;
import java.util.ArrayList;
import java.util.Comparator;
public class GenericExercise {
public static void main(String[] args) {
ArrayList<Employee> employees = new ArrayList<>();
employees.add(new Employee("tom", 12000, new MyDate(2000, 12, 12)));
employees.add(new Employee("milan", 20000, new MyDate(2001, 11, 11)));
employees.add(new Employee("tom", 18000, new MyDate(2000, 10, 10)));
employees.sort(new Comparator<Employee>() {
@Override
public int compare(Employee emp1, Employee emp2) {
if (!(emp1 instanceof Employee && emp2 instanceof Employee)) {
System.out.println("类型不正确...");
return 0;
}
//比较name
int i = emp1.getName().compareTo(emp2.getName());
if (i != 0) {
return i;
}
//比较birthday
int yearMinus = emp1.getBirthday().getYear() - emp2.getBirthday().getYear();
if (yearMinus != 0) {
return yearMinus;
}
int monthMinus = emp1.getBirthday().getMonth() - emp2.getBirthday().getMonth();
if (monthMinus != 0) {
return monthMinus;
}
return emp1.getBirthday().getDay() - emp2.getBirthday().getDay();
}
});
System.out.println("employees=" + employees);
}
}
优化:
package com.tk.generic;
public class MyDate implements Comparable<MyDate> {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = 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;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
@Override
public int compareTo(MyDate o) {
//比较birthday
int yearMinus = year - o.getYear();
if (yearMinus != 0) {
return yearMinus;
}
int monthMinus = month - o.getMonth();
if (monthMinus != 0) {
return monthMinus;
}
return day - o.getDay();
}
}
package com.tk.generic;
import java.util.ArrayList;
import java.util.Comparator;
public class GenericExercise {
public static void main(String[] args) {
ArrayList<Employee> employees = new ArrayList<>();
employees.add(new Employee("tom", 12000, new MyDate(2000, 12, 12)));
employees.add(new Employee("milan", 20000, new MyDate(2001, 11, 11)));
employees.add(new Employee("tom", 18000, new MyDate(2000, 10, 10)));
employees.sort(new Comparator<Employee>() {
@Override
public int compare(Employee emp1, Employee emp2) {
if (!(emp1 instanceof Employee && emp2 instanceof Employee)) {
System.out.println("类型不正确...");
return 0;
}
//比较name
int i = emp1.getName().compareTo(emp2.getName());
if (i != 0) {
return i;
}
return emp1.getBirthday().compareTo(emp2.getBirthday());
}
});
System.out.println("employees=" + employees);
}
}
练习二
User类:
package com.homework;
public 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 + '\'' +
'}';
}
}
DAO:
package com.homework;
import java.util.*;
public class DAO<T> {
private Map<String, T> map = new HashMap<>();
public T get(String id) {
return map.get(id);
}
public void update(String id, T entity) {
map.put(id, entity);
}
public List<T> list() {
ArrayList<T> list = new ArrayList<>();
// list.add((T) map.values());
Set<String> keySet = map.keySet();
for (String key : keySet) {
list.add(get(key));
}
return list;
}
public void delete(String id) {
map.remove(id);
}
public void save(String id, T entity) {
map.put(id, entity);
}
}
package com.homework;
import org.junit.jupiter.api.Test;
public class Homework01 {
public static void main(String[] args) {
}
@Test
public void testList() {
DAO<User> userDAO = new DAO<>();
userDAO.save("001", new User(1, 11, "jack"));
userDAO.save("002", new User(2, 12, "tom"));
userDAO.save("003", new User(3, 13, "milan"));
System.out.println(userDAO.list());
userDAO.update("003", new User(3, 30, "milan"));
System.out.println(userDAO.list());
userDAO.delete("001");
System.out.println(userDAO.list());
System.out.println(userDAO.get("003"));
}
}