泛型限定

泛型的通配符:?

可以对类型进行限定:? extends E :接收E类型或者E的子类型对象。上限
? super E:接收E类型或者E的父类型。下限
使用同一个方法操作泛型不同的对象时:< ? >
ArrayList和LinkedList的父类是Collection,故在操作时, printCollection(Collection<?> al)应该是Collection

package com.vv.generic.advance.demo;

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

import com.vv.bean.Student;
import com.vv.bean.Worker;

public class GenericAdvanceDemo {

    public static void main(String[] args) {

        ArrayList<Worker> al = new ArrayList<Worker>();
        al.add(new Worker("worker",12));
        al.add(new Worker("worker2",32));
        
        LinkedList<Student> al2 = new LinkedList<Student>();
        al.add(new Worker("student",12));
        al.add(new Worker("student2",12));
        
        printCollection(al);
        printCollection(al2);
    }

    private static void printCollection(Collection<?> al) {
        Iterator<?> it = al.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        
    }

}

当方法时静态的时候,泛型限定也可以改为:此时it.next()就可以有返回值,但若是?形式,则不能进行it.next()

    private static <l> void printCollection(Collection<l> al) {
        Iterator<l> it = al.iterator();
        while(it.hasNext()){
            l str = it.next();
            System.out.println(it.next());
        }

如上,一般都采用第一种,只有当需要操作返回值时才需要进行使用第二种:

private static <l> l printCollection(Collection<l> al) {
        
        Iterator<l> it = al.iterator();
        l str = it.next();
        return str;
    }

如何使用泛型操作一部分,即只能存储person的子类,即泛型的限定;像LinkedList<Integer> al3 = new LinkedList<Integer>();
这种方式进行添加数据时编译会报错,因为只能存储Person的子类,

package com.vv.generic.advance.demo;

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

import com.vv.bean.Person;
import com.vv.bean.Student;
import com.vv.bean.Worker;

public class GenericAdvanceDemo {

    public static void main(String[] args) {

        ArrayList<Worker> al = new ArrayList<Worker>();
        al.add(new Worker("worker",12));
        al.add(new Worker("worker2",32));
        
        LinkedList<Student> al2 = new LinkedList<Student>();
        al.add(new Worker("student",12));
        al.add(new Worker("student2",12));
        
        LinkedList<Integer> al3 = new LinkedList<Integer>();
        al3.add(5);
        al3.add(5);
        
        printCollection(al);
        printCollection(al2);
        printCollection(al3);

    }

    private static  void  printCollection(Collection<? extends Person> al) {
        
        Iterator<? extends Person> it = al.iterator();
    
        while(it.hasNext()){
            System.out.println(it.next());
        }
        
    }

}

上限:
如下情况,迭代器接收数据时,就可以进行使用Person父类进行数据的输出

package com.vv.generic.advance.demo;

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

import com.vv.bean.Person;
import com.vv.bean.Student;
import com.vv.bean.Worker;

public class GenericAdvanceDemo {

    public static void main(String[] args) {

        ArrayList<Worker> al = new ArrayList<Worker>();
        al.add(new Worker("worker",12));
        al.add(new Worker("worker2",32));
        
        LinkedList<Student> al2 = new LinkedList<Student>();
        al.add(new Worker("student",12));
        al.add(new Worker("student2",12));
    
        printCollection(al);
        printCollection(al2);


    }

    private static  void  printCollection(Collection<? extends Person> al) {
        
        Iterator<? extends Person> it = al.iterator();
    
        while(it.hasNext()){
            Person p = it.next();
//          System.out.println(it.next());
            System.out.println(p.getName()+p.getAge());
        }
        
    }

}

下限:
即泛型中存储时只能存储Student和它的父类

private static  void  printCollection(Collection<? super Student> al) {
        
        Iterator<? super Student> it = al.iterator();
    
        while(it.hasNext()){
            System.out.println(it.next());
        }
        
    }

一般在存储元素的时候都是使用上限,因为这样取出都是按照上限类型进行运算的,不会出现类型安全隐患


泛型上限.png

如下 ArrayList<Student> al4 = new ArrayList<Student>();
将泛型定义为Person时才可以编译成功,
上限:存什么用什么类型进行接收

package com.vv.generic.advance.demo;

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

import com.vv.bean.Person;
import com.vv.bean.Student;
import com.vv.bean.Worker;

public class GenericAdvanceDemo {

    public static void main(String[] args) {
        
        ArrayList<Worker> al1 = new ArrayList<Worker>();
        al1.add(new Worker("worker",12));
        al1.add(new Worker("worker2",32));
        
        LinkedList<Student> al2 = new LinkedList<Student>();
        al2.add(new Student("student",12));
        al2.add(new Student("student2",2));
        
        ArrayList<Person> al3 = new ArrayList<Person>();
        al3.add(new Person("person",11));
        al3.add(new Person("person2",21));
        
        ArrayList<Student> al4 = new ArrayList<Student>();
        al4.addAll(al1);
    }

}

下限:


泛型下限.png

通常对集合中的元素进行取出操作时,即取什么类型使用什么的父类进行接收

package com.vv.generic.advance.demo;

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

import com.vv.bean.Person;
import com.vv.bean.Student;
import com.vv.bean.Worker;

public class GenericAdvanceDemo {

    public static void main(String[] args) {
        
        TreeSet<Worker> al1 = new TreeSet<Worker>(new CompByName());
        al1.add(new Worker("worker",12));
        al1.add(new Worker("worker2",32));
        
        TreeSet<Student> al2 = new TreeSet<Student>(new CompByName());
        al2.add(new Student("student",12));
        al2.add(new Student("student2",2));
        
        TreeSet<Person> al3 = new TreeSet<Person>(new CompByName());
        al3.add(new Person("person",11));
        al3.add(new Person("person2",21));
        
        TreeSet<Person> al4 = new TreeSet<Person>();
//      al4.addAll(al2);
//      al4.addAll(al1);
//      al4.addAll(al3);
//      System.out.println(al4.size());
        
        Iterator<Student> it = al2.iterator();
        
        while(it.hasNext()){
//          Person p = it.next();
            System.out.println(it.next());
//          System.out.println(p.getName()+p.getAge());
        }

    }

    
        
    }
class CompByName implements Comparator<Person>{

    @Override
    public int compare(Person o1, Person o2) {
        
        int temp = o1.getName().compareTo(o2.getName());
        
        return temp==0? o1.getAge()-o2.getAge():temp;
    }
    
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 自定义泛型 1.1、泛型的定义介绍 在集合中,不管是接口还是类,它们在定义的时候类或接口名的后面都使用<标识符>,...
    Villain丶Cc阅读 8,857评论 0 7
  • Scala与Java的关系 Scala与Java的关系是非常紧密的!! 因为Scala是基于Java虚拟机,也就是...
    灯火gg阅读 3,489评论 1 24
  • 一、基础知识:1、JVM、JRE和JDK的区别:JVM(Java Virtual Machine):java虚拟机...
    杀小贼阅读 2,415评论 0 4
  • 月光把天空照亮,撒下一片光芒点缀海洋,每当流星从天而降,心中的梦想都随风飘扬。展开透明翅膀越出天窗,找寻一个最美丽...
    变好的小花生阅读 183评论 0 0
  • 早晨7点半离开宿舍,近晚上9点才回来。流连忘返于一个讲座连着一个讲座。 跟老师紧密接触一整天,前几天...
    石筒子阅读 201评论 0 0