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:
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. 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);" cause my second object is of the type AbstractDTO and not of a class which extends AbstractDTO.
Can someone help me with that? Thx alot ;)