美文网首页Java
Java19-5.2 泛型通配符及限定上下线

Java19-5.2 泛型通配符及限定上下线

作者: 第二套广播体操 | 来源:发表于2019-01-15 12:13 被阅读0次
public class Generic_Test6 {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("abc1");
        list.add("abc2");
        list.add("abc3");
        list.add("abc4");
        printCollection(list);
        Set<String> set=new TreeSet<>();
        set.add("abc1");
        set.add("abc2");
        set.add("abc3");
        set.add("abc4");
        printCollection(set);
    }

    private static void printCollection(Collection<String> collection) {
        for (Iterator<String> iterator = collection.iterator(); iterator.hasNext(); ) {
            String s = iterator.next();
            System.out.println(s);
        }
    }
}

为了同时可以迭代两个集合 我们将迭代范围改到Collection
但是此时我们依然无法迭代类型为学生的集合

 List<Student> list1=new ArrayList<>();
        list1.add(new Student());
        list1.add(new Student());
        list1.add(new Student());
        list1.add(new Student());
        printCollection(list1);

所以我们引入通配符<?>

//通配符<?>不明确传递参数类型
    private static void printCollection(Collection<?> collection) {
        for (Iterator<?> iterator = collection.iterator(); iterator.hasNext(); ) {
            Object o = iterator.next();
            System.out.println(o);
        }
    }

上限应用


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

public class Gerneric_Test7 {
    public static void main(String[] args) {
        ArrayList<MStudent> list=new ArrayList<>();
        list.add(new MStudent("as",20));
        list.add(new MStudent("a2",10));
        list.add(new MStudent("as",19));
        TreeSet<Student> set=new TreeSet<Student>(list);
        for (Iterator<Student> iterator = set.iterator(); iterator.hasNext(); ) {
            Student next = iterator.next();
            System.out.println(next);
        }

    }
}

下限应用

public class Generic_Test8 {
    public static void main(String[] args) {
        Comparator<Student> comparator=new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int temp=o1.getName().compareTo(o2.getName());
                return temp==0? o1.getAge()-o2.getAge():temp;
            }
        };
        TreeSet<MStudent> set1=new TreeSet<>(comparator);
        set1.add(new MStudent("asd1",12));
        set1.add(new MStudent("zxcw",15));
        set1.add(new MStudent("xczx",13));
        set1.add(new MStudent("awqs",18));
        TreeSet<HStudent> set2=new TreeSet<>(comparator);
        set2.add(new HStudent("zxcz",12));
        set2.add(new HStudent("qwes",14));
        set2.add(new HStudent("zxwq",15));
        set2.add(new HStudent("zxcw",23));
        TreeSet<Student> set3=new TreeSet<>(comparator);
        set3.addAll(set1);
        set3.addAll(set2);
        for (Iterator<Student> iterator = set3.iterator(); iterator.hasNext(); ) {
            Student next = iterator.next();
            System.out.println(next);
        }

        }
    }

相关文章

  • Java19-5.2 泛型通配符及限定上下线

    为了同时可以迭代两个集合 我们将迭代范围改到Collection但是此时我们依然无法迭代类型为学生的集合 所以我们...

  • java基础

    八、泛型 面试题==什么是泛型中的限定通配符和非限定通配符 ?这是另一个非常流行的Java泛型面试题。限定通配符对...

  • java泛型

    1:问:什么是 Java 泛型中的限定通配符和非限定通配符?有什么区别? 答:限定通配符对类型进行限制,泛型中有两...

  • 泛型机制(二)

    泛型通配符 实例代码 结果 泛型的上下限限定 定义

  • java基础知识梳理&泛型初探

    目录 概述 范型的使用 类型参数 类型通配符 泛型方法 泛型类 限定类型参数上限 上界通配符(Upper Boun...

  • 通配符的超类型限定

    带有超类型限定的通配符可以向泛型对象写入, 带有子类型限定的通配符可以从泛型对象读取. 编译器只知道需要某个 Em...

  • 通配符详解 extends super

    在java泛型中,? 表示通配符,代表未知类型,< ? extends Object>表示上边界限定通配符,< ?...

  • 泛型、型变与投影

    简单泛型 kotlin 对于简单泛型的支持与java类似, 可以通过通配符,提升代码的灵活度 限定型泛型 在编码实...

  • JAVA泛型•通配符限定

    为了更好地阅读体验,欢迎访问博客原文 引一个例子 考虑一个这样的场景,计算数组中的最大元素。 仔细看看code01...

  • 9.集合框架

    泛型 1.泛型通配符?如果不知道使用什么类型来接收数据的时候,可以使用 ?表示未知 2.泛型上限和下限用来限定元素...

网友评论

    本文标题:Java19-5.2 泛型通配符及限定上下线

    本文链接:https://www.haomeiwen.com/subject/yioedqtx.html