java <E> 在 Collection<E> 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1056820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does <E> mean in Collection<E>?
提问by Johanna
What meaning has <E>on the code Collection<E>?
<E>代码上有什么含义Collection<E>?
回答by Konrad Rudolph
It means that you're dealing with a collection of items with type E. Imagine you've got a cup of tea. Instead of tea, it could also hold coffee so it makes sense to describe the cup as a generic entity:
这意味着您正在处理类型为 的项目集合E。想象一下,你有一杯茶。除了茶,它还可以盛放咖啡,因此将杯子描述为一个通用实体是有道理的:
class Cup<T> { …?}
now you could fill it, either with coffee or tea (or something else):
现在你可以用咖啡或茶(或其他东西)填充它:
Cup<Tea> cuppa = new Cup<Tea>();
Cup<Coffee> foamee = new Cup<Coffee>();
In order for this to work, both Teaand Coffeewould need to be types defined in your program as well.
为了使这一工作,既Tea和Coffee将需要在你的程序中定义以及类型。
This is a compile-time constrainton your code. Coming back from the (rather useless) cup example, collections (arrays, lists …) usually contain items of onetype, e.g. integers or strings. Generics help you to express this in Java:
这是对您的代码的编译时约束。从(相当无用的)杯子示例中回来,集合(数组、列表……)通常包含一种类型的项目,例如整数或字符串。泛型可帮助您在 Java 中表达这一点:
Collection<String> strList = new ArrayList<String>();
strList.add("Foobar"); // Works.
strList.add(42); // Compile error!
Notice the compile error above? You only get this when using generics. The following code also works, but would not give the nice error message:
注意到上面的编译错误了吗?你只有在使用泛型时才会得到这个。以下代码也有效,但不会给出很好的错误消息:
Collection strList = new ArrayList();
strList.add("Foobar"); // Works.
strList.add(42); // Works now. Do we really want this?!
回答by Vinko Vrsalovic
It's the use of generics. Check this introout. And then don't forget to read this tutorial.
An excerpt follows (which compares the use of a cast versus the use of generics):
摘录如下(比较了强制转换的使用与泛型的使用):
When you see the code <Type>, read it as “of Type”; the declaration above reads as “Collection of String c.” The code using generics is clearer and safer. We have eliminated an unsafe cast and a number of extra parentheses. More importantly, we have moved part of the specification of the method from a comment to its signature, so the compiler can verify at compile time that the type constraints are not violated at run time. Because the program compiles without warnings, we can state with certainty that it will not throw a ClassCastException at run time. The net effect of using generics, especially in large programs, is improved readability and robustness.
当你看到代码 <Type> 时,将其读作“of Type”;上面的声明读作“字符串 c 的集合”。使用泛型的代码更清晰、更安全。我们消除了不安全的强制转换和一些额外的括号。更重要的是,我们已将方法规范的一部分从注释移至其签名,因此编译器可以在编译时验证在运行时未违反类型约束。因为程序编译时没有警告,我们可以肯定地说它不会在运行时抛出 ClassCastException。使用泛型的最终效果,尤其是在大型程序中,是提高了可读性和健壮性。
For example, the interface of a List is
比如一个List的接口是
public interface List<E> {
void add(E x);
Iterator<E> iterator();
}
This means you can build a list whose contents are all of the same explicit type (not only of type Object), even if you have defined the type yourself. So, if you create a Name class you can write
这意味着您可以构建一个列表,其内容都是相同的显式类型(不仅是 Object 类型),即使您自己定义了类型。所以,如果你创建一个 Name 类,你可以写
List<Name> nameList = new ArrayList<>();
and then fill it with Name instances and directly retrieve Name instances from it without having to cast or otherwise worry about it because you'll always get either a Name instance or null back, never an instance of a different type.
然后用 Name 实例填充它并直接从中检索 Name 实例而无需强制转换或以其他方式担心它,因为您将始终获得 Name 实例或 null 返回,而不是不同类型的实例。
More importantly, you cannot insert anything different from a Name instance in such a List, because it will fail at compile time.
更重要的是,您不能在此类 List 中插入与 Name 实例不同的任何内容,因为它会在编译时失败。
nameList.add(false); //Fails!
nameList.add(new Name("John","Smith")); //Succeeds supposing Name has a
//firstName, lastName constructor

