简体   繁体   中英

A NavigableSet<String> does not match Collection<Object> in method argument

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>) 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?

Collection<String> is not a subtype of Collection<Object> , therefore the compiler doesn't find any compatible methods. Put

public <T> void foo(Collection<T> c) {
  for (T o : c) bar(o);
}

The inheritance relationship between T<A> and T<B> is is called "generic covariance". It is not as simple as, if A inherits from B , then T<A> inherits from T<B> , because the type parameter may be "in" or "out" (as C# would put it).

See for example http://www.ibm.com/developerworks/java/library/j-jtp01255/index.html or java generics covariance or http://etymon.blogspot.co.uk/2007/02/java-generics-and-covariance-and.html

Marko's suggestion is the simplest fix for your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM