简体   繁体   中英

Java Generics always for collections

I have a question about Java Generics. 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)?

thanks,

No. It just means that your type is parameterized with the type String. There are plenty of non-collection generic types. See Callable<T> , Future<T> , Comparator<T> for example.

You should start from the beginning: 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.

No it does not! It only mean that Test is parametrized with the String.

Read oracle tutorial for good understanding of Generics.

No.

To do what you mean you add extends Collection<T> to your class declaration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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