简体   繁体   English

Java泛型始终用于集合

[英]Java Generics always for collections

I have a question about Java Generics. 我对Java泛型有疑问。 If I define a class like this: 如果我定义这样的类:

public Test<String> ...

Does this mean that my class now acts like a collection type for Strings? 这是否意味着我的类现在就像字符串的集合类型一样? For example when seeing it like this List I know that it is a List of Strings. 例如,当看到这样的列表时,我知道它是一个字符串列表。 Does the <> always mean it's a collection (general meaning, not the actual Collection type)? <>是否总是表示它是一个集合(一般含义,而不是实际的Collection类型)?

thanks, 谢谢,

No. It just means that your type is parameterized with the type String. 否。这仅表示您的类型使用String类型进行了参数化。 There are plenty of non-collection generic types. 有很多非集合通用类型。 See Callable<T> , Future<T> , Comparator<T> for example. 例如,请参阅Callable<T>Future<T>Comparator<T>

You should start from the beginning: http://docs.oracle.com/javase/tutorial/java/generics/generics.html 您应该从头开始: http : //docs.oracle.com/javase/tutorial/java/generics/generics.html

Long story short: generics are a completely different concept from collections. 长话短说:泛型是与集合完全不同的概念。 Java collections use generics since they were introduced, but you can still use non-generics versions of them. Java集合自引入以来就使用泛型,但是您仍然可以使用它们的非泛型版本。

No it does not! 不,不是的! It only mean that Test is parametrized with the String. 这仅表示Test已使用String参数化。

Read oracle tutorial for good understanding of Generics. 阅读oracle 教程,以更好地理解泛型。

No. 没有。

To do what you mean you add extends Collection<T> to your class declaration 按照您的意思,您将extends Collection<T>添加到类声明中

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

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