简体   繁体   中英

How to interpret and use generic vector?

Can any body explain this generic vector Vector<? super Object> list = ... Vector<? super Object> list = ... and where to use it?

It is the same as

Vector<Object> list

Usually "? super Type" means that you can add supertypes of type Type

Object is the highest level in Java so there are no supertypes so the ? super is not needed

If it was

Vector<? extends Object> list

then it would mean that you can add any object that is a subclass of Object. This is every object in Java so it would be the same as

Vector<Object> list

Also consider List rather than Vector and if you want it thread safe then wrap it in Collections.synchronizedList(...)

Here you are defining the lower bound of your unknown object ? . So the Vector<? super Object Vector<? super Object can contain only Object and any super class of Object . But since Object does'nt have a super class it has no sense. It behaves same as Vector<Object> .

Refer this sample .

The counterpart for this is upper bound Vector<? extends Object> Vector<? extends Object> where you can add any object that extends Object .

You should be able to avoid using Object as the generic type is most cases.

<? super Object> <? super Object> is absurd since there is no super type of Object, but it is allowed, and it consists of just Object types. http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html

这样的列表只能包含Object类型的元素,因为Object没有任何超类型,类似于使用带有像String这样的不可变类的扩展,其中这样的集合只能包含字符串。

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