泛型

来源:互联网 发布:淘宝店铺 花呗 编辑:程序博客网 时间:2024/06/06 03:21

泛型

Generic JDK1.5(包含)以后出现的一种安全机制

泛型的出现,让我们的集合操作更加安全

泛型的出现,让程序出错的时间,从运行时期,提前到了编译时期

泛型出现之前,程序人员只能主观判断集合存储对象的数据类型

有了泛型,泛型就是强制的保证集合存储数据类型的唯一性

泛型避免了强制类型转换的麻烦

格式

       类<数据类型> 变量 = new 类<数据类型>();

什么时候使用泛型,当我们要使用一个类的时候,发现这个类的右边有<>

code
package cn.river.generic;import java.util.ArrayList;import java.util.Iterator;public class GenericDemo {public static void main(String[] args) {ArrayList<String> al = new ArrayList<String>();al.add("river01");al.add("river02");al.add("river03");al.add("river04");al.add("river05");System.out.println(al);Iterator<String> it = al.iterator();while(it.hasNext()){System.out.println(it.next());}}}
code2

package cn.river.generic;import java.util.ArrayList;import java.util.Iterator;public class GenericDemo1 {public static void main(String[] args) {ArrayList<Person> al = new ArrayList<Person>();al.add(new Person("river01",21));al.add(new Person("river02",22));al.add(new Person("river03",23));al.add(new Person("river04",24));Iterator<Person> it = al.iterator();while(it.hasNext()){Person p = it.next();System.out.println(p.name+"..."+p.age);}}}
code3

package cn.river.generic;import java.util.HashSet;import java.util.Iterator;public class GenericDemo2 {public static void main(String[] args) {HashSet<Person> hs = new HashSet<Person>();hs.add(new Person("river01",21));hs.add(new Person("river02",22));hs.add(new Person("river02",22));hs.add(new Person("river05",24));hs.add(new Person("river03",23));hs.add(new Person("river03",23));hs.add(new Person("river04",24));Iterator<Person> it = hs.iterator();while(it.hasNext()){Person p = it.next();System.out.println(p.name+"..."+p.age);}}}
code4

package cn.river.generic;import java.util.Iterator;import java.util.TreeSet;public classGenericDemo3 {   public static void main(String[] args) {      //TreeSet<Person> ts = newTreeSet<Person>();      TreeSet<Person>ts = newTreeSet<Person>(new MyComparator());      ts.add(new Person("river01",21));      ts.add(new Person("river02",22));      ts.add(new Person("river03",23));      ts.add(new Person("river02",22));      ts.add(new Person("river03",23));      ts.add(new Person("river04",24));      Iterator<Person>it = ts.iterator();      while(it.hasNext()){         Personp = it.next();         System.out.println(p.name+"..."+p.age);      }   }}
code5

package cn.river.generic;import java.util.*;/* * 定义一个类,类中有一个方法,可以建立一个类的对象 * 泛型类 */public class GenericDemo4 {public static void main(String[] args) {Factory<Student> f = new Factory<Student>();f.setObj(new Student());Student s = f.getObj();System.out.println(s);Factory<Worker> fw = new Factory<Worker>();fw.setObj(new Worker());Worker w = fw.getObj();System.out.println(s);}}class Student{}class Worker{}class Factory<T>{private T obj;public void setObj(T obj){this.obj = obj;}public T getObj(){return obj;}}
code6

package cn.river.generic;class Test{public <T> void show(T s){System.out.println(s);}public static <T> void method(T s){System.out.println(s);}}public class GenericDemo5 {public static void main(String[] args) {Test t = new Test();t.show(false);Test.method(2.3);}}
code7

package cn.river.generic;import java.util.Comparator;public class MyComparator implements Comparator<Person>{@Overridepublic int compare(Person o1, Person o2) {// TODO Auto-generated method stubint num = o1.name.compareTo(o2.name);return num==0 ? o1.age-o2.age : num;}}
code8

package cn.river.generic;public class Person implements Comparable<Person>{String name;int age;public Person(String name, int age){this.name=name;this.age=age;}public String toString(){return this.name+","+this.age;}public int hashCode(){return this.name.hashCode()+this.age*31;}public boolean equals(Object obj){if(obj==this) return true;Person p = (Person)obj;return this.name.equals(this.name) && this.age == age;}@Overridepublic int compareTo(Person o) {int num=this.age - o.age;return num==0 ? this.name.compareTo(o.name) : num;}}
原创粉丝点击