简体   繁体   English

Java 通用符号,<e> 放<e></e></e>

[英]Java generic notation, <E> Set <E>

The following Java code is used to create a set in Java:以下 Java 代码用于在 Java 中创建集合:

    Set<String> fromUi = Set.of("val1", "val2", "val3", "val4");

Which in term calls this code:术语称为此代码:

static <E> Set<E> of(E e1, E e2, E e3, E e4) {
        return new ImmutableCollections.SetN<>(e1, e2, e3, e4);
    }

What does the "double" use of the type parameter mean?类型参数的“双重”使用是什么意思? ie can we not just say Set<E> instead of <E> Set<E> ?即我们不能只说Set<E>而不是<E> Set<E>吗?

can we not just say Set<E> instead of <E> Set<E> ?我们不能只说Set<E>而不是<E> Set<E>吗?

No, because then the type variable E wouldn't be declared.不,因为那时类型变量E不会被声明。

This isn't a "double" use:这不是“双重”用途:

  • The first <E> is the type variable declaration第一个<E>是类型变量声明
  • The second <E> is part of the type of the Set<E> which is the return type of the method: it's a Set whose elements are of type E , and to which E s can be added.第二个<E>Set<E>类型的一部分,它是方法的返回类型:它是一个Set ,其元素的类型为E ,并且可以添加E s。

Declaring one or more type variables on a method makes the method a generic method .在方法上声明一个或多个类型变量会使该方法成为泛型方法 Instance methods can access type variables from the surrounding class, or declare their own;实例方法可以从周围的class中访问类型变量,或者声明自己的; static methods cannot access type variables from the surrounding class, and so must always declare their own. static 方法不能访问来自周围 class 的类型变量,因此必须始终声明自己的。

// Generic class, E is accessible in instance methods/initializers/constructors.
class MyClass<E> {
  // Non-generic method, uses the E from the class.
  Set<E> someSet() { ... } 

  // Generic method, declares its own type variable.
  <M> Set<M> someSet1() { ... } 

  // Generic method, declares its own type variable which hides
  // the E on the class (bad idea).
  <E> Set<E> someSet2() { ... } 

  // Generic method, must declare its own type variable.
  static <E> Set<E> someStaticSet() { ... } 
}

// Non-generic classes can declare generic methods.
class MyClass {
  // Generic method, declares its own type variable.
  <M> Set<M> someSet1() { ... } 

  // Generic method, must declare its own type variable.
  static <E> Set<E> someStaticSet() { ... } 
}
static <E> Set<E> of(E e1, E e2, E e3, E e4) {

You can read it as: whatever 'E' is (for any type 'E'), pass 4 parameters of this type and get a Set of this type as a result.您可以将其解读为:无论 'E' 是什么(对于任何类型 'E'),传递 4 个此类型的参数并得到一个此类型的 Set 作为结果。

Your method is static .您的方法是static It won't be able to access type variables declared from its class, hence it needs to declare its own <type> declaration.它将无法访问从其 class 声明的类型变量,因此它需要声明自己的<type>声明。 So its not double declaration.所以它不是双重声明。

The first <E> is where static method declares which type it uses
The second with Set<E> to specify the type of elements in a Set. 

you won't need to use this kind of declaration if your non static method uses same generic <type> declared by class.如果您的非 static 方法使用由 class 声明的相同通用<type> ,则不需要使用这种声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM