[英]Java Generics extends Loop
how can i loop through object which extends from a class? 我如何遍历从类扩展的对象? i give an example: I have an Abstract class (AbstractDTO) and 2 classes which extends AbstractDTO: 我举一个例子:我有一个Abstract类(AbstractDTO)和2个扩展AbstractDTO的类:
public abstract class AbstractDTO
{
private int id;
/**
* @return the id
*/
public int getId()
{
return id;
}
/**
* @param id the id to set
*/
public void setId(int id)
{
this.id = id;
}
}
public class FirstDTO extends AbstractDTO
{
}
public class SecondDTO extends AbstractDTO
{
}
Now i got a method which excpects 2 Collections of objects which extends AbstractDTO. 现在,我有了一种可以扩展2对象集合的方法,该对象集合扩展了AbstractDTO。 I want to add all objects of the second list to the first list, if they are not in the first list already. 我想将第二个列表中的所有对象添加到第一个列表中,如果它们不在第一个列表中。 how can i do this? 我怎样才能做到这一点?
i tried the following: 我尝试了以下方法:
public static void summarizeCollection(List<? extends AbstractDTO> firstList, Set<? extends AbstractDTO> secondList)
{
for(AbstractDTO second : secondList)
{
boolean exists = false;
for (AbstractDTO first : firstList)
{
if (second.getId() == first.getId())
{
exists = true;
}
}
if(!exists)
{
firstList.add(second);
}
}
}
I got an error in the line " firstList.add(second);" 我在“ firstList.add(second);”行中出错。 cause my second object is of the type AbstractDTO and not of a class which extends AbstractDTO. 因为我的第二个对象是AbstractDTO类型,而不是扩展AbstractDTO的类。
Can someone help me with that? 有人可以帮我吗? Thx alot ;) 多谢 ;)
The compiler cannot be sure that the elements in secondList
are of the same type of the elements declared in firstList
, thus you get a compiler error here: 编译器不能肯定的是,在元素secondList
都是同一类型中声明的元素的firstList
,所以你在这里得到一个编译错误:
firstList.add(second);
This is caused for the definition of the arguments: 这是由于参数的定义引起的:
List<? extends AbstractDTO> firstList, Set<? extends AbstractDTO> secondList
If you're sure both List
and Set
are of the same type and this type extends AbstractDTO
, then declare your method like this: 如果您确定List
和Set
属于同一类型,并且此类型扩展了AbstractDTO
,则可以这样声明您的方法:
public static <T extends AbstractDTO> void summarizeCollection(List<T> firstList, Set<T> secondList)
Then, inside the method, change this for
loop: 然后,在方法内部,将其更改for
循环:
for(AbstractDTO second : secondList)
into 进入
for(T second : secondList)
And on. 等等。
More info: 更多信息:
The compiler error is there because the compiler cannot guarantee that the wildcard from the List
is equal to the wildcard from the Set
. 出现编译器错误是因为编译器无法保证List
中的通配符等于Set
的通配符。
You can solve this by making the method generic. 您可以通过使方法通用来解决此问题。 You need a type variable to represent the type of the List
, which must be an AbstractDTO
, say T
. 您需要一个类型变量来表示List
的类型,它必须是AbstractDTO
,例如T
You can make the Set
a wildcard bounded by T
, so that a Set
of any type that is T
or extends T
can have its elements added to the List
. 您可以将Set
为以T
为边界的通配符,以便任何类型为T
或扩展T
的Set
都可以将其元素添加到List
。
public static <T extends AbstractDTO> void summarizeCollection(
List<T> firstList, Set<? extends T> secondList)
Then both for
loops' variables should be of type T
. 然后,两个for
循环的变量都应为T
类型。
for(T second : secondList)
and 和
for (T first : firstList)
This will allow the call to add
to compile. 这将允许调用add
进行编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.