您的位置:首页 > 其它

集合框架-泛型

2016-05-24 17:33 387 查看
泛型:
1.JDK1.5版本出现的新特性,安全机制。
2.泛型将运行时期的问题转移到了编译时期。
3.避免了强制转换的麻烦。
4.泛型技术是用于编译时期的技术。

泛型的体现:
<> 这就是用于定义类型参数的符号。
泛型可以简单的理解为,接收具体的元素类型。

泛型-擦除:
对源码进行编译时,通过泛型进行类型的检查。如果类型没有问题,则将源码编译成class文件。注意,class文件中是不带有泛型信息的。这种情况称之为泛型擦除。虚拟机会根据输入数据的类型进行类型的转换
package cn.itcast.p1.generic;

import java.util.ArrayList;
import java.util.Iterator;

public class GenericDemo1 {

/**
* @param args
*/
public static void main(String[] args) {
//String[] arr = new String[3];//先明确类型。
//arr[0] = new Integer(34);//存储时,如果类型不匹配,就编译报错。
ArrayList<String> al = new ArrayList<String>();
al.add("abcd");//add(Object obj);
al.add("cba");//add(Object obj);
al.add("cba");//add(Object obj);
//al.add(5);//自动装箱,一旦指定了具体元素的类型,存储时不匹配,就编译失败。
for(Iterator<String> it = al.iterator(); it.hasNext(); ){
//Object obj = it.next();
String s = it.next();
System.out.println(s.length());
}
}

}


集合框架-泛型-泛型类
package cn.itcast.p1.generic;

import cn.itcast.bean.Person;

public class GenericDemo2 {
public static void main(String[] args) {
Person p = new Person();

Tool tool = new Tool();
tool.setObject(p);
//tool.setObject("abc");//运行时才会报错
Person pp = (Person)tool.getObject();
pp.getName();
Util<Person> u = new Util<Person>();
u.setObject(p);
//u.setObject("abc");//编译时期,自动报错
Person ppp = u.getObject();
}

}


//有了泛型以后,这个类就可以重新设计了。
//泛型类。较以前避免了强制麻烦,还提高了安全。

class Util<QQ>{
private QQ obj;
public void setObject(QQ obj){
this.obj = obj;
}
public QQ getObject(){
return obj;
}
}


集合框架-泛型-泛型方法

package cn.itcast.bean;

public class GenericDemo3 {
public static void main(String[] args) {

Demo3<String> d3 = new Demo3<String>();
d3.show("abc");
d3.print("haha");
d3.method("abcde");
d3.method(new Integer(56));
Demo3<Integer> d33 = new Demo3<Integer>();
d33.print(8);
//d33.show("hehe");
d33.method("hehe");
}

}


/*
* 类中操作的对象类型不确定,将泛型定义在类上。
* 方法中操作的类型不确定,将泛型定义在方法上。
* 如果是在静态方法上使用泛型,该泛型必须定义在方法上。
*/

class Demo3<W>{
public void show(W w){
System.out.println("show :"+w);
}
public void print(W w){
System.out.println("print:"+w);
}

//* 将泛型定义在方法上。
public <M> void method(M m){
System.out.println("method:"+m);
}

//* 将泛型定义在静态方法上。
public static <K> void function(K w){
System.out.println("function:"+w);
}
}


集合框架-泛型-泛型接口

package cn.itcast.p1.generic;

public class GenericDemo4 {
public static void main(String[] args) {

//1,
Demo d = new Demo();
d.show("haha");
//2
Demo2<String> d2 = new Demo2<String>();
d2.show("haha");
}

}

// 泛型接口。
interface Inter<U>{
public void show(U u);
}

//1,
class Demo implements Inter<String>{
@Override
public void show(String u) {
}
}

//2,
class Demo2<T> implements Inter<T>{
@Override
public void show(T u) {
}
}


29-API-集合框架-泛型-通配符
package cn.itcast.p2.generic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class GenericTest {
public static void main(String[] args) {

ArrayList<String> al = new ArrayList<String>();
al.add("abc1");
al.add("abc2");
al.add("abc3");
ArrayList<Integer> al2 = new ArrayList<Integer>();
al2.add(45);
al2.add(67);
al2.add(22);
printCollection(al);
printCollection(al2);
}

//建立一个专门用打印集合元素的方法。

//1,
public static <T> void printCollection(Collection<T> coll){
for(Iterator<T> it = coll.iterator(); it.hasNext(); ){
T t = it.next();
System.out.println(t.toString());
}
}
//2,
public static  void printCollection(Collection<?> coll){
for(Iterator<?> it = coll.iterator(); it.hasNext(); ){
System.out.println(it.next().toString());
}
}
}

T代表一个具体类型,可以用对象调用该类型。
?问号只能在泛型中进行标识未知类型

集合框架-泛型-泛型的限定
* 泛型的限定。
* ? extends E :接收E类型或者E的子类型。
?是E的子类
* ? super E : 接收E类型或者E的父类型
?是E的父类
package cn.itcast.p1.generic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import cn.itcast.bean.Person;
import cn.itcast.bean.Student;
import cn.itcast.bean.Worker;

public class GenericDemo5 {
public static void main(String[] args) {

/* 泛型的限定。
*
* ? extends E :接收E类型或者E的子类型。 //只能? 不能其他的字母
*
* ? super E : 接收E类型或者E的父类型。*/

ArrayList<Person> al = new ArrayList<Person>();
al.add(new Person("lisi1",21));
al.add(new Person("lisi2",22));
al.add(new Person("lisi3",23));
show(al);
ArrayList<Student> al2 = new ArrayList<Student>();
al2.add(new Student("xiaoming1",21));
al2.add(new Student("xiaoming2",22));
al2.add(new Student("xiaoming3",23));
//show(al2);
ArrayList<String> al3 = new ArrayList<String>();
al3.add("abc1");
al3.add("abc2");
al3.add("abc3");
//show(al3);
ArrayList<Worker> al4 = new ArrayList<Worker>();
al4.add(new Worker("dachui1",21));
al4.add(new Worker("dachui1",22));
al4.add(new Worker("dachui1",23));
show(al4);
}

public static void show(Collection<? super Worker> coll){
for (Iterator<? super Worker> it = coll.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}

}

package cn.itcast.bean;

public class Student extends Person {

public Student() {
super();
}

public Student(String name, int age) {
super(name, age);
}

@Override
public String toString() {
return "Student [name=" + getName() + ", age=" + getAge() + "]";
}
}


集合框架-泛型-通配符API的体现

类型不确定,equals(Object obj)方法,参数是Object,任何类型的参数都能添加
/*
* 了解api中的泛型的体现。
* boolean containsAll(Collection<?> c)
*/
containsAll(Collection<?> c)
如果此 collection 包含指定 collection 中的所有元素,则返回 true。
package cn.itcast.p2.generic;

import java.util.ArrayList;
import java.util.Iterator;

import cn.itcast.bean.Person;
import cn.itcast.bean.Student;

public class GenericTest2 {
public static void main(String[] args) {

ArrayList<String> a1 = new ArrayList<String>();
a1.add("abc1");
a1.add("abc2");
a1.add("abc3");

ArrayList<String> a2 = new ArrayList<String>();
a2.add("abc1");
a2.add("abc2");

ArrayList<Integer> a3 = new ArrayList<Integer>();
a3.add(45);
a3.add(89);

boolean b = a1.containsAll(a3);
System.out.println("b="+b);
}
}

interface MyCollection<T>{

boolean myContainsAll(MyCollection<?> c);

boolean addAll(MyCollection<? extends T> c);
}
集合框架-泛型-上限API的体现
addAll(Collection<? extends E> c)
将指定 collection 中的所有元素都添加到此 collection 中(可选操作)
/*
* 了解api中的泛型上限。 ? extends E
*
* boolean addAll(Collection<? extends E> c)
*
*/
ArrayList<Person> a4 = new ArrayList<Person>();
a4.add(new Person("lisi1",21));
a4.add(new Person("lisi2",22));
a4.add(new Person("lisi3",23));

ArrayList<Student> a5 = new ArrayList<Student>();
a5.add(new Student("小明4",24));
a5.add(new Student("小明5",25));
a5.add(new Student("小明6",26));
a4.addAll(a5);

Iterator<Person> it = a4.iterator();
while(it.hasNext()){
Person p = it.next();
System.out.println(p.getName()+":"+p.getAge());
}
}


集合框架-泛型-下限API的体现
添加元素的时候一般用上限 AddAll(Collection<? extends E> c)
取出元素的 时候一般用到下限,取出的元素是学生,可以用Person来接收

TreeSet(Comparator<? super E> comparator)
构造一个新的空 TreeSet,它根据指定比较器进行排序。
package cn.itcast.p2.generic;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

import cn.itcast.bean.Person;
import cn.itcast.bean.Student;
import cn.itcast.bean.Worker;

public class GenericTest3 {
public static void main(String[] args) {

TreeSet<Student> ts = new TreeSet<Student>(new ComparatorByAge());
ts.add(new Student("xiaoming3",23));
ts.add(new Student("xiaoming8",21));
ts.add(new Student("xiaoming2",22));
ts.add(new Student("xiaoming9",20));

Iterator<Student> it = ts.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println(s);
}

TreeSet<Worker> ts2 = new TreeSet<Worker>(new ComparatorByAge());
ts2.add(new Worker("dachui2",23));
ts2.add(new Worker("dachui9",21));
ts2.add(new Worker("dachui0",22));
ts2.add(new Worker("dachui6",20));

Iterator<Worker> it2 = ts2.iterator();
while(it2.hasNext()){
Worker s = it2.next();
System.out.println(s);
}
}
}

class ComparatorByAge implements Comparator<Person>{
//此时用Person类型,Person类型和其子类型都可以使用compare方法。
//Comparator<Worker>表示Worker和Worker的父类可以使用compare()
//Comparator<? super E>E类型及E类型父类
@Override
public int compare(Person o1, Person o2) {
int temp = o1.getAge() - o2.getAge();
return temp==0?o1.getName().compareTo(o2.getName()):temp;
}
}

class MyTreeSet<Q>{
MyTreeSet(Comparator<? super Q> comp){
}
}

集合框架-泛型-常见问题
定义的泛型的类型是不是安全来思考。
package cn.itcast.p2.generic;
import java.util.ArrayList;

public class GenericTest4 {
public static void main(String[] args) {

//ArrayList<Animal> al1 = new ArrayList<Dog>();
//al1.add(new Dog());
//al1.add(new Cat());

//ArrayList<Dog> al2 = new ArrayList<Animal>();
//只要左右两边一致,这是最简单的思考方式。

}
//public static void show(Collection<?> coll){
//coll.add("haha");
//coll.add(4);
//}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: