在集合中使用泛型
package com.www.java1;
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 year + "-" + month + "-" + day;
}
@Override
public int compareTo(MyDate o) {
int minYear = this.getYear() - o.getYear();
if(minYear != 0){
return minYear;
}
int minMonth = this.getMonth() - o.getMonth();
if(minMonth != 0){
return minMonth;
}
return this.getDay() - o.getDay();
}
}
package com.www.java1;
public class Employee implements Comparable<Employee>{
private String name;
private int age;
private MyDate birthday;
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;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
@Override
public int compareTo(Employee o) {
return this.name.compareTo(o.name);
}
}
package com.www.java1;
import org.junit.Test;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetTest {
@Test
public void test(){
MyDate myDate = new MyDate(1995, 6, 30);
MyDate myDate1 = new MyDate(1995, 10, 30);
MyDate myDate2 = new MyDate(1996, 8, 9);
MyDate myDate3 = new MyDate(1994, 1, 25);
MyDate myDate4 = new MyDate(1993, 3, 7);
Employee employee = new Employee("张三", 27, myDate);
Employee employee1 = new Employee("李四", 27, myDate1);
Employee employee2 = new Employee("王五", 26, myDate2);
Employee employee3 = new Employee("赵六", 28, myDate3);
Employee employee4 = new Employee("钱三", 29, myDate4);
TreeSet<Employee> set = new TreeSet<Employee>();
set.add(employee);
set.add(employee1);
set.add(employee2);
set.add(employee3);
set.add(employee4);
Iterator<Employee> iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
Comparator com = new Comparator<Employee>(){
@Override
public int compare(Employee o1, Employee o2) {
MyDate date1 = o1.getBirthday();
MyDate date2 = o2.getBirthday();
return date1.compareTo(date2);
}
};
TreeSet<Employee> set1 = new TreeSet(com);
set1.add(employee);
set1.add(employee1);
set1.add(employee2);
set1.add(employee3);
set1.add(employee4);
System.out.println("**********");
Iterator<Employee> iterator1 = set1.iterator();
while(iterator1.hasNext()){
System.out.println(iterator1.next());
}
}
}
package com.www.java;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Iterator;
public class GenericTest {
@Test
public void test(){
ArrayList list = new ArrayList();
list.add(23);
list.add(83);
list.add(12);
list.add(95);
list.add(100);
list.add(72);
list.add("Tom");
for(Object obj:list){
Integer i = (Integer) obj;
System.out.println(i);
}
}
@Test
public void test1(){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(42);
list.add(63);
list.add(72);
for(Integer score : list){
System.out.println(score);
}
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}}
自定义泛型类
package com.www.java;
import java.util.ArrayList;
import java.util.List;
public class Order<T> {
String name;
int age;
T orderT;
public Order() {
T[] arr = (T[])new Object[10];
}
public Order(String name, int age, T orderT) {
this.name = name;
this.age = age;
this.orderT = orderT;
}
public T getOrderT() {
return orderT;
}
public void setOrderT(T orderT) {
this.orderT = orderT;
}
@Override
public String toString() {
return "Order{" +
"name='" + name + '\'' +
", age=" + age +
", orderT=" + orderT +
'}';
}
public<E> List<E> copyFronArrayToList(E[] arr){
ArrayList<E> arrayList = new ArrayList<>();
for(E e:arr){
arrayList.add(e);
}
return arrayList;
}
}
package com.www.java;
public class SubOrder extends Order<Integer>{
}
package com.www.java;
public class SubOrder1<T> extends Order<T>{
}
package com.www.java;
import org.junit.Test;
import java.util.List;
public class GenericTest1 {
@Test
public void test(){
Order order1 = new Order();
order1.setOrderT(123);
order1.setOrderT("哈哈");
Order<String> order = new Order<>("Tom",1100,"心情不好");
order.setOrderT("心情不错");
System.out.println(order);
}
@Test
public void test2(){
SubOrder subOrder = new SubOrder();
SubOrder1<String> subOrder1 = new SubOrder1<>();
}
@Test
public void test3(){
Order<String> order = new Order<>();
Integer[] arr = new Integer[]{1,2,3,4};
String[] arr1 = new String[]{"Tom"};
List<Integer> list = order.copyFronArrayToList(arr);
System.out.println(list);
}
}
共性–特性–操作目标
package com.www.java2;
public class Customer {
}
package com.www.java2;
import java.util.List;
public class DAO <E>{
public void add(E e){
}
public boolean delet(int index){
return false;
}
public void update(int index,E e){
}
public E getIndex(int index){
return null;
}
public List<E> getForList(int index){
return null;
}
}
package com.www.java2;
public class CustomerDAO extends DAO <Customer> {
}
package com.www.java3;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class GerericTest {
@Test
public void test(){
Object obj = null;
String str = null;
obj = str;
Object[] arr1 = null;
String[] arr2 = null;
arr1 = arr2;
List<Object> list1 = null;
List<String> list2 = null;
}
@Test
public void test1(){
List<Object> list1 = null;
List<String> list2 = null;
List<?> list3 = null;
list3 = list1;
list3 = list2;
show(list1);
show(list2);
list2.add("Aa");
list2.add("BB");
Object o = list3.get(0);
System.out.println(o);
}
public void show(List<?> list){
Iterator<?> iterator = list.iterator();
while(iterator.hasNext()){
Object obj = iterator.next();
System.out.println(obj);
}
}
public void test2(){
List<? extends Person> list1 = null;
List<? super Person> list2 = null;
List<Students> list3 = new ArrayList<>();
List<Person> list4 = new ArrayList<>();
List<Object> list5 = new ArrayList<>();
list1 = list3;
list1 = list4;
list2 = list4;
list2 = list5;
list1 = list3;
Person person = list1.get(0);
list1 = list4;
Person person1 = list1.get(0);
Object person2 = list1.get(0);
list2 = list4;
Object object = list2.get(0);
list2.add(new Person());
list2.add(new Students());
}
}
package com.www.java3;
public class Person {
}
package com.www.java3;
public class Students extends Person{
}
|