I have a method which takes a Collection<Object>
where the Object
can be a String
or CustomClass
. It then takes each element of the collection and passes it to a method with an argument of Object
like so:
public void foo(Collection<Object> c) {
for(Object o : c)
bar(o);
}
public void bar(Object o) {
if(o instanceof String || o instanceof CustomClass) {
...
}
}
bar
works fine when I pass it a String
or CustomClass
, but when I try to pass a NavigableSet<String>
to foo
I get cannot find symbol; symbol : method foo(java.util.NavigableSet<java.lang.String>)
.
However if I change the the argument type in foo
to Collection<String>
it works fine, but this means I need to make a new foo(Collection<CustomClass>)
method which will involve repeating code. Is there a way around this?