Java: Unable to fix unchecked conversion required java.util.Collection<? extends java.lang.String>

StackOverflow https://stackoverflow.com/questions/18272033

I have a Vector of Strings, oldV, and want a second Vector that is identical but has all duplicates removed. The following works, in the sense that it compiles and results in Vector that has the duplicates removed:

Vector<String> newV = new Vector<String>( new LinkedHashSet(oldV) );

However, it generates two unchecked conversion warnings:

> warning: [unchecked] unchecked call to LinkedHashSet(java.util.Collection<? extends E>) as a member of the raw type java.util.LinkedHashSet

> warning: [unchecked] unchecked conversion
> found   : java.util.LinkedHashSet
> required: java.util.Collection<? extends java.lang.String>

In both cases, the ^ is positioned directly under the "new" in "new LinkedHashSet.

I'm at a loss of how to fix these warnings.

有帮助吗?

解决方案

If oldV is declared as a Vector<String> just use the parameterized version of 'LinkedHashSet':

Vector<String> newV = new Vector<String>( new LinkedHashSet<String>(oldV) )

you can also use the annotation @SuppressWarnings if you just want to get rid of the warnings:

@SuppressWarnings("unchecked")
Vector<String> newV = new Vector<String>( new LinkedHashSet(oldV) );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top