gpt4 book ai didi

java - 方法签名中的 是什么?

转载 作者:行者123 更新时间:2023-12-02 06:04:02 24 4
gpt4 key购买 nike

我正在从 here 阅读有关 Set 接口(interface)的内容下面的代码是从集合中删除重复项的通用方法。

我的问题是那是什么 **< E>**放置在 static 之后 Set<E> 之前?

我的意思是不是 Set<E>足够 ?为什么<E>有两次吗?

public static <E> Set<E> removeDups(Collection<E> c) {
return new LinkedHashSet<E>(c);
}

最佳答案

这里**<E>**是一个泛型类型。泛型类型定义为

A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept. LINK

关于您与 <E> 相关的问题。可以在同一个教程中找到对其的详细描述

类型参数命名约定

By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.

The most commonly used type parameter names are:

 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

You'll see these names used throughout the Java SE API and the rest of this lesson.

关于java - 方法签名中的 <E> 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22433904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com