When I call Set.class.isAssignableFrom(Iterable.class), it returns false.
Nevertheless, in the docs, java.util.Set is listed as a subinterface of java.lang.Iterable. Hence my confusion. You can even try it out in a single line of code:
System.out.println(Set.class.getName() + " is " + ((Set.class.isAssignableFrom(Iterable.class)) ? "" : "NOT " ) + "assignable from " + Iterable.class.getName());
it prints java.util.Set is NOT assignable from java.lang.Iterable.
Why is that?
That's because you're using isAssignableFrom wrong.
As the docs say, isAssignableFrom(Class<?> cls) "determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter". So cls would be Set.class, and the full syntax would be:
Iterable.class.isAssignableFrom(Set.class).
...which, indeed, returns true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With