Java官方笔记8泛型

news2024/9/20 6:02:55

泛型

为什么需要泛型?generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods.

说白了就像Python动态语言的变量,是动态的,可以指向任意类型。

泛型有个好处是不需要类型转换:

List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);
List<String> list = new ArrayList<String>();
list.add("hello");
String s = list.get(0);   // no cast

这个例子的泛型,是指List的实现使用了泛型<T>,从而给使用带来了好处。

定义

class name<T1, T2, ..., Tn> { /* ... */ }

比如定义class List<T>,使用List<String>

将以下代码:

public class Box {
    private Object object;

    public void set(Object object) { this.object = object; }
    public Object get() { return object; }
}

改为泛型实现:

/**
 * Generic version of the Box class.
 * @param <T> the type of the value being boxed
 */
public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}

T必须是非基本数据类型:any class type, any interface type, any array type, or even another type variable

type parameter命名采用单个大写字母:

  • E - Element (used extensively by the Java Collections Framework)
  • K - Key
  • N - Number
  • T - Type
  • V - Value
  • S, U, V etc. - 2nd, 3rd, 4th types

这样能很好的跟其他命名区分开来。

使用泛型,必须要指定具体的值,比如这里的Integer:

Box<Integer> integerBox;

这就跟方法调用传参是一个道理。

Diamond,将:

Box<Integer> integerBox = new Box<Integer>();

简写为:

Box<Integer> integerBox = new Box<>();

多个type parameters

public interface Pair<K, V> {
    public K getKey();
    public V getValue();
}

public class OrderedPair<K, V> implements Pair<K, V> {

    private K key;
    private V value;

    public OrderedPair(K key, V value) {
    this.key = key;
    this.value = value;
    }

    public K getKey()    { return key; }
    public V getValue() { return value; }
}
Pair<String, Integer> p1 = new OrderedPair<String, Integer>("Even", 8);  // 这里的int类型8,自动装箱为了I
Pair<String, String>  p2 = new OrderedPair<String, String>("hello", "world");

简写为diamond:

OrderedPair<String, Integer> p1 = new OrderedPair<>("Even", 8);
OrderedPair<String, String>  p2 = new OrderedPair<>("hello", "world");

嵌套:

OrderedPair<String, Box<Integer>> p = new OrderedPair<>("primes", new Box<Integer>(...));

Raw Types

raw type is the name of a generic class or interface without any type arguments.

比如:

public class Box<T> {
    public void set(T t) { /* ... */ }
    // ...
}
Box<Integer> intBox = new Box<>();
Box rawBox = new Box();  // 这个就是rawType

在IDEA有时候会碰到警告Raw use of parameterized class 'List',就是指的这个玩意。这是老式写法,raw types会绕过泛型的type checks,应该避免使用。

Generic Methods

定义,泛型位置在return type的前面:

public class Util {
    public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) {
        return p1.getKey().equals(p2.getKey()) &&
               p1.getValue().equals(p2.getValue());
    }
}

public class Pair<K, V> {

    private K key;
    private V value;

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public void setKey(K key) { this.key = key; }
    public void setValue(V value) { this.value = value; }
    public K getKey()   { return key; }
    public V getValue() { return value; }
}

使用:

Pair<Integer, String> p1 = new Pair<>(1, "apple");
Pair<Integer, String> p2 = new Pair<>(2, "pear");
boolean same = Util.<Integer, String>compare(p1, p2);

调用泛型方法时也可以省略泛型入参:

Pair<Integer, String> p1 = new Pair<>(1, "apple");
Pair<Integer, String> p2 = new Pair<>(2, "pear");
boolean same = Util.compare(p1, p2);  // 这里省略了泛型入参

Bounded Type Parameters

有点像Python的typing,限制动态变量的类型,使用extends关键字:

public class Box<T> {

    private T t;          

    public void set(T t) {
        this.t = t;
    }

    public T get() {
        return t;
    }

    public <U extends Number> void inspect(U u){
        System.out.println("T: " + t.getClass().getName());
        System.out.println("U: " + u.getClass().getName());
    }

    public static void main(String[] args) {
        Box<Integer> integerBox = new Box<Integer>();
        integerBox.set(new Integer(10));
        integerBox.inspect("some text"); // error: this is still String!
    }
}

这样还能进一步调用bounded type parameters的方法:

public class NaturalNumber<T extends Integer> {

    private T n;

    public NaturalNumber(T n)  { this.n = n; }

    public boolean isEven() {
        return n.intValue() % 2 == 0;  // intValue()是Integer的方法
    }

    // ...
}

Multiple Bounds

Class A { /* ... */ }
interface B { /* ... */ }
interface C { /* ... */ }

class D <T extends A & B & C> { /* ... */ }

class的位置必须在interface前面。

Bounded Type Parameters的用途之一,比如:

public static <T> int countGreaterThan(T[] anArray, T elem) {
    int count = 0;
    for (T e : anArray)
        if (e > elem)  // compiler error
            ++count;
    return count;
}

会编译报错,因为>符号只适用基本数据类型,如果想支持Object,怎么办呢:

public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
    int count = 0;
    for (T e : anArray)
        if (e.compareTo(elem) > 0)
            ++count;
    return count;
}

extends Comparable<T>以后调用compareTo()方法,就能既支持基本数据类型又能支持Object了。

泛型在继承时有个注意的点: Box<Integer> and Box<Double> are not subtypes of Box<Number>

正确的方式:

You can subtype a generic class or interface by extending or implementing it.

interface PayloadList<E,P> extends List<E> {
  void setPayload(int index, P val);
  ...
}

Type Inference

类型推断:Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable.(换个理解方式,就是动态变量需要知道绑定哪个类型)

Diamond就是一种Type Inference:

Map<String, List<String>> myMap = new HashMap<>();

在构造方法中进行推断:

class MyClass<X> {
  <T> MyClass(T t) {
    // ...
  }
}
MyClass<Integer> myObject = new MyClass<>("");

X推断为Integer,T推断为String。

Lambda Expressions也会根据上下文推断target type:

public static void printPersons(List<Person> roster, CheckPerson tester)
public void printPersonsWithPredicate(List<Person> roster, Predicate<Person> tester)
printPersons(
        people, 
        p -> p.getGender() == Person.Sex.MALE
            && p.getAge() >= 18
            && p.getAge() <= 25);  // 自动推断为CheckPerson
printPersonsWithPredicate(
        people,
        p -> p.getGender() == Person.Sex.MALE
             && p.getAge() >= 18
             && p.getAge() <= 25);)  // 自动推断Predicate<Person>
  • Variable declarations
  • Assignments
  • Return statements
  • Array initializers
  • Method or constructor arguments
  • Lambda expression bodies
  • Conditional expressions, ?:
  • Cast expressions

再看个例子:

public interface Runnable {
    void run();
}

public interface Callable<V> {
    V call();
}
void invoke(Runnable r) {
    r.run();
}

<T> T invoke(Callable<T> c) {
    return c.call();
}
String s = invoke(() -> "done");  // Lambda

实际推断使用哪个?答案是Callable,因为它有return,而Runnable没有。

Wildcards

使用?extends表示上限:

List<? extends Number>

可以是 List<Integer>List<Double>, and List<Number>

这里的extends既是class的extends,也是interface的implements。

List<Object>List<?>有什么区别?

①子类型

public static void printList(List<Object> list) {
    for (Object elem : list)
        System.out.println(elem + " ");
    System.out.println();
}

it prints only a list of Object instances; it cannot print List<Integer>List<String>List<Double>, and so on, because they are not subtypes of List<Object>.

public static void printList(List<?> list) {
    for (Object elem: list)
        System.out.print(elem + " ");
    System.out.println();
}

Because for any concrete type AList<A> is a subtype of List<?>, you can use printList() to print a list of any type.

②值

You can insert an Object, or any subtype of Object, into a List<Object>. But you can only insert null into a List<?>.

使用?super表示下限:

public static void addNumbers(List<? super Integer> list) {
    for (int i = 1; i <= 10; i++) {
        list.add(i);
    }
}

可以是List<Integer>List<Number>, and List<Object> — anything that can hold Integer values

?能支持集合子类型:

Type Erasure

Type Erasure是Java编译器为了实现泛型做的:

  • Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
  • Insert type casts if necessary to preserve type safety.
  • Generate bridge methods to preserve polymorphism in extended generic types.

Restriction on Generics

1、不能使用基本数据类型:

class Pair<K, V> {

    private K key;
    private V value;

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    // ...
}
Pair<int, char> p = new Pair<>(8, 'a');  // compile-time error

只能使用包装类:

Pair<Integer, Character> p = new Pair<>(8, 'a');

2、不能创建泛型实例:

public static <E> void append(List<E> list) {
    E elem = new E();  // compile-time error
    list.add(elem);
}

只能通过类来创建实例:

public static <E> void append(List<E> list, Class<E> cls) throws Exception {
    E elem = cls.newInstance();   // OK
    list.add(elem);
}
List<String> ls = new ArrayList<>();
append(ls, String.class);

3、static不能使用泛型

public class MobileDevice<T> {
    private static T os;  // 在实例化后,会同时代表3种类型,显然不合理

    // ...
}
MobileDevice<Smartphone> phone = new MobileDevice<>();
MobileDevice<Pager> pager = new MobileDevice<>();
MobileDevice<TabletPC> pc = new MobileDevice<>();

4、不能instanceof

public static <E> void rtti(List<E> list) {
    if (list instanceof ArrayList<Integer>) {  // compile-time error
        // ...
    }
}

使用?可以:

public static void rtti(List<?> list) {
    if (list instanceof ArrayList<?>) {  // OK; instanceof requires a reifiable type
        // ...
    }
}

5、不能创建泛型数组:

List<Integer>[] arrayOfLists = new List<Integer>[2];  // compile-time error

6、Cannot Create, Catch, or Throw Objects of Parameterized Types

// Extends Throwable indirectly
class MathException<T> extends Exception { /* ... */ }    // compile-time error

// Extends Throwable directly
class QueueFullException<T> extends Throwable { /* ... */ // compile-time error
public static <T extends Exception, J> void execute(List<J> jobs) {
    try {
        for (J job : jobs)
            // ...
    } catch (T e) {   // compile-time error
        // ...
    }
}

throws可以:

class Parser<T extends Exception> {
    public void parse(File file) throws T {     // OK
        // ...
    }
}

7、重载方法不能有擦除后相同的泛型:

public class Example {
    public void print(Set<String> strSet) { }
    public void print(Set<Integer> intSet) { }  // 编译错误
}

参考资料:

Generics https://dev.java/learn/generics/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/656515.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

有没有不限制群发数量的软件?

父亲节的由来 父亲节&#xff08;Fathers Day&#xff09;&#xff0c;顾名思义是感恩父亲的节日。 世界上第一个父亲节&#xff0c;1910年诞生于美国。 而中国的父亲节起源要追溯到民国时代。民国三十四年的八月八日&#xff08;1945.8.8&#xff09;&#xff0c;上海文人所…

git-在当前分支做变基(rebase current onto selected)

文章目录 git rebase变基git rebase使用场景git rebase还是git merge?在当前分支做变基使用总结 git rebase变基 初学git&#xff0c;在合并分支上必定会常用到 git merge 语法。git除了使用合并(merge)集成2个分支之间的更改&#xff0c;还有另外一种称为rebase的方法。 gi…

R -- corrplot包

文章目录 常用参数简介method测试部分type参数测试diag参数测试order参数测试 常用参数简介 corrplot包常用参数介绍&#xff1a; corr: 必需参数&#xff0c;表示相关性矩阵&#xff0c;可以是数据框或者矩阵。 method: 表示绘制相关性矩阵的方法&#xff0c;有以下几种取值…

H5语义化标签

语义化标签 HTML5引入了一组语义化标签,这些标签旨在更好地描述网页内容的结构和含义。通过使用这些标签,可以提高网页的可读性、可访问性、搜索引擎优化,以及代码的可维护性。以下是对HTML5语义化标签的详细解释: <header>:用于定义页面或区块的页眉部分,通…

centos8 KDC认证

20国赛公开卷KDC做法&#xff08;个人整理&#xff09; 先根据题意&#xff0c;用chrony服务同步时间 Linux-1为KDC服务器&#xff0c;认证Linux-2和Linux-3.三台都先同步时间。 所有的Linux selinux都为enforcing&#xff0c;防火墙都是打开的。此次为临时测试整理&#xff…

Java常见面试题之SSM | Spring Boot(不定时更新)

IOC DI机制 IOC的意思是控制反转,控制的意思啊由Spring 工厂控制对象,反转是从我们主动创建好对象反转为等待Spring 工厂的注入 DI是依赖注入,组件需要的依赖对象由容器注入,主要通过setter 构造器 接口三种注入方式注入. AOP 面向切面编程 AOP思想是做无侵入式的功能增强,即在…

English Learning - L3 作业打卡 Lesson6 Day40 2023.6.13 周二

English Learning - L3 作业打卡 Lesson6 Day40 2023.6.13 周二 引言&#x1f349;句1: As they are expected to be hungry after their long journey, food is laid out for them.成分划分弱读连读爆破语调 &#x1f349;句2: Specially-made lanterns are hung outside each…

ASEMI代理光宝高速光耦LTV-6341特征,LTV-6341应用

编辑-Z LTV-6341参数描述&#xff1a; 型号&#xff1a;LTV-6341 储存温度Tstg&#xff1a;-55~ 150℃ 工作温度Topr&#xff1a;-40~ 125℃ 总输出电源电压(VCC –VEE)&#xff1a;35V 平均正向输入电流IF&#xff1a;25mA 反向输入电压VR&#xff1a;5V 输入电流&…

第15章_锁

第15章_锁 事务的 隔离性 由这章讲述的 锁 来实现。 1. 概述 锁是计算机协调多个进程或线程并发访问某一资源的机制。在程序开发中会存在多线程同步的问题&#xff0c;当多个线程并发访问某个数据的时候&#xff0c;尤其是针对一些敏感的数据&#xff08;比如订单、金额等)&…

老板喜欢表现型管理者,而不是实干型管理者?

最近热搜榜&#xff1a;月薪几万运营总监只会管鸡毛蒜皮 很多老板在招聘方面往往会有些着急&#xff0c;他们可能会把同样的项目交给两个团队去完成&#xff0c;然后比较两个团队的表现。 其中&#xff0c;t1团队的负责人老王&#xff0c;整天忙于拍视频、发朋友圈、写汇报&am…

2023年江西省赣州市职业院校技能大赛(中职组) 网络安全竞赛试题(6月17日竞赛真题))

2023年江西省赣州市职业院校技能大赛&#xff08;中职组&#xff09; 网络安全竞赛试题 三、竞赛任务书内容 &#xff08;一&#xff09;拓扑图 &#xff08;二&#xff09;A模块基础设施设置/安全加固&#xff08;200分&#xff09; 一、项目和任务描述&#xff1a; 假定你…

宾利添越升级ACC自适应巡航+抬头显示HUD+Naim音响案例分享

大家好&#xff0c;我是小志bzs878&#xff0c;专注名车原厂升级&#xff0c;欢迎戳戳右上角“”号关注一下&#xff0c;持续为您带来精彩改装案例 今天装车一台宾利添越&#xff0c;几百个W的气场非常强大的豪车还需要加装什么项目&#xff1f; 车子还未上牌&#xff0c;今天…

python---------xpath提取数据------打破局限

作者前言 欢迎小可爱们前来借鉴我的gtiee秦老大大 (qin-laoda) - Gitee.com 目录 为什么要学习XPATH和LXML类库 什么是XPATH 认识XML XML的节点关系 常⽤节点选择⼯具 节点选择语法 节点修饰语法 选择未知节点 lxml库 _____________________________________________…

层次分析法(MATLAB)

对之前的学习进行总结&#xff0c;整个比赛下来好像就用到了这个方法&#xff0c;最后也不知道对不对&#xff0c;反正最后还有点赶&#xff0c;就是很懵的那种&#xff0c;对于层次分析话的还是有点了解了&#xff0c;由于是纯小白&#xff0c;有错误的地方希望各位大佬能够指…

微服务远程调用openFeign整合

✅作者简介&#xff1a;大家好&#xff0c;我是Cisyam&#xff0c;热爱Java后端开发者&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f34e;个人主页&#xff1a;Cisyam-Shark的博客 &#x1f49e;当前专栏&#xff1a; 微服务探索之旅 ✨特色专…

go详细环境配置(windows)

下载go 安装包 官网&#xff1a; https://go.dev/dl/ 下载压缩包&#xff0c;解压 环境变量配置 GOROOT 配置你解压的目录 在Path中追加一条 %GOROOT%\bin 这里go对比JAVA还多了一个配置&#xff1a; GOPATH 配置go以后项目的地址 并在自己新建的GOPATH路径文件夹下新建三…

微服务中常见问题

Spring Cloud 组件 Spring Cloud五大组件有哪些&#xff1f; Eureka&#xff1a;注册中心 Ribbon&#xff1a;负载均衡 Feign&#xff1a;远程调用 Hystrix&#xff1a;服务熔断 Zuul/Gateway&#xff1a;服务网关 随着SpringCloud Alibaba在国内兴起&#xff0c;我们项目中…

MySQL安装教程(2023年,4月)

一、MySQL下载&#xff08;安装版&#xff09; 1、进入MySQL官网 官网地址&#xff1a;https://www.mysql.com/ 2、点击【DOWNLOADS】。 3、向下滑&#xff0c;点击【MySQL Community (GPL) Downloads】下载MySQL社区版。 4、点击【MySQL Installer for Windows】下载MySQL安…

pandas解决数据缺失、重复的方法与实践

1. 数据缺失 常见的数据缺失是指一条数据记录中&#xff0c;某个数据项没有值&#xff0c;延申到实际应用中&#xff0c;还有一种时间序列的缺失&#xff0c;例如按整点采集数据&#xff0c;缺少某一时刻的数据&#xff08;缺少一整行数据&#xff09;。 解决方法&#xff0c…

Type-C PD显示器方案简介

方案概述 LDR6020 Type-C PD显示器方案可以给显示器提供一个全功能C口&#xff0c;支持手机&#xff0c;电脑&#xff0c;游戏主机等一线投屏功能&#xff0c;同时支持PD快充输出。LDR6020内置了 USB Power Delivery 控制器和 PD BMC PHY 收发器&#xff0c;支持PD2.0/3.0等快…