我正在使用一个扩展LinkedHashSet的集合类。通过迭代器或forEach遍历此集合的效果很好,但简单的for循环则无法实现。我在做什么错了?
public class MySet<E> extends LinkedHashSet
{
//..........
}
MySet<String> serie = new MySet();
// this doesn't work: Incompatible types: Object cannot be converted to String
for (String entry : serie) {
System.out.println(entry);
}
// this works
serie.forEach((entry) -> {
System.out.println(entry);
});
// this works too
for (Iterator<String> it = serie.iterator(); it.hasNext();) {
String entry = it.next();
System.out.println(entry);
}