集合框架-泛型-萬用字元的體現
阿新 • • 發佈:2021-10-24
1 package cn.itcast.p5.generic.advance.demo; 2 3 import java.util.TreeSet; 4 import java.util.ArrayList; 5 import java.util.Collection; 6 import java.util.Comparator; 7 import java.util.Iterator; 8 import java.util.TreeSet; 9 10 11 import cn.itcast.p2.bean.Person; 12 import cn.itcast.p2.bean.Student;GenericAdvanceDemo613 import cn.itcast.p2.bean.Worker; 14 15 public class GenericAdvanceDemo6 { 16 17 public static void main(String[] args) { 18 // TODO Auto-generated method stub 19 ArrayList<Person> al1 = new ArrayList<Person>(); 20 21 al1.add(new Person("abc",30)); 22al1.add(new Person("abc4",34)); 23 24 ArrayList<Person> al2 = new ArrayList<Person>(); 25 26 al2.add(new Person("abc222222",30)); 27 al2.add(new Person("abc422222222",34)); 28 29 al1.containsAll(al2); 30 31 ArrayList<String> al4 = newArrayList<String>(); 32 33 al4.add("abcdeef"); 34 al4.add("abc"); 35 36 al1.containsAll(al4); 37 38 "abc".equals(new Person("asdasd",20));//任意物件都具備equals,equals能比較任意物件 39 40 41 Iterator<Person> it = al1.iterator(); 42 while(it.hasNext()) { 43 System.out.println(it.next()); 44 } 45 46 47 } 48 49 public static void printCollection(Collection<?> al) { 50 Iterator<?> it = al.iterator(); 51 52 while(it.hasNext()) { 53 System.out.println(it.next().toString());//一般情況下只要呼叫全是Object方法就可以用這個? 54 } 55 } 56 57 58 } 59 60 class MyCollection2<E>{//方法定義時可以定義接受E型別 61 public boolean containsAll(Collection/*<E>*/<?> coll) {//你新傳入的型別不知道,一般情況下只要呼叫全是Object方法就可以用這個? 62 return true; 63 } 64 } 65 //有的時候返回的一個集合不確定型別,可以用?接收
1 package cn.itcast.p2.bean; 2 3 public class Person implements Comparable<Person>{ 4 private String name; 5 private int age; 6 7 8 public Person() { 9 super(); 10 // TODO Auto-generated constructor stub 11 } 12 13 public Person(String name, int age) { 14 super(); 15 this.name = name; 16 this.age = age; 17 } 18 public int compareTo(Person p) {//指定比較型別,傳入比較型別 19 // Person p= (Person)obj; 20 int temp = this.age-p.age; 21 return temp==0?this.name.compareTo(p.name):temp; 22 } 23 24 25 // @Override 26 // public boolean equals(Object obj) {//不能把Object改為Person,equals方法來自Object,Object沒有定義泛型 27 // // TODO Auto-generated method stub 28 // if (this == obj) { 29 // return true; 30 // } 31 // if (!(obj instanceof Person)) { 32 // throw new RuntimeException() 33 // } 34 // Person p = (Person)obj; 35 // return super.equals(obj); 36 // } 37 /* 38 * @Override public int hashCode() { final int prime = 31; int result = 1; 39 * result = prime * result + age; result = prime * result + ((name == null) ? 0 40 * : name.hashCode()); return result; } 41 * 42 * @Override public boolean equals(Object obj) { if (this == obj) return true; 43 * if (obj == null) return false; if (getClass() != obj.getClass()) return 44 * false; Person other = (Person) obj; if (age != other.age) return false; if 45 * (name == null) { if (other.name != null) return false; } else if 46 * (!name.equals(other.name)) return false; return true; }//alt+shift+s 47 * hashCode和equals 48 */ 49 public String getName() { 50 return name; 51 } 52 public void setName(String name) { 53 this.name = name; 54 } 55 public int getAge() { 56 return age; 57 } 58 public void setAge(int age) { 59 this.age = age; 60 } 61 62 @Override 63 public String toString() { 64 return "Person:"+getName()+":"+getAge(); 65 } 66 67 68 }Person