Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is java.util.Set not Assignable From java.lang.Iterable?

Tags:

java

iterable

set

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?

like image 927
Aldán Creo Avatar asked Nov 08 '25 01:11

Aldán Creo


1 Answers

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.

like image 61
Aldán Creo Avatar answered Nov 10 '25 17:11

Aldán Creo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!