Avoiding instanceOf when finding index of an interface within a list I am working on some code and I have come across the following:

private int getPosition() {
     List&lt;IDescriptor&gt; fragmentDescriptors = new ArrayList&lt;IDescriptor&gt;(mDescriptors);

     int position = -1;

     for (IDescriptor fragmentDescriptor : fragmentDescriptors) {
         if (fragmentDescriptor instanceof VideoDescriptor) {
                position = fragmentDescriptors.indexOf(fragmentDescriptor);
         }
     }

     return position;
}


In a situation like this, how can one find an index of a specific implementation within a list without using instanceOf?

Update:
Few points: mDesciptors is a set. The point of the question was: The set of IDescriptor is injected into the class, the class has no idea what the set will contain, it does however know there maybe a IDescriptor that is meant for video.. the problem is the current method relies on VideoDescriptor, therefore if that implementation changes, the position will no longer be found.

I'd like a general review of this.  The method was really just to demonstrate the dependency on VideoDescriptor.
I totally agree with Uri and have some further comments:


  the class has no idea what the set will contain, it does however know there maybe a IDescriptor that is meant for video..


I'm sorry, but I have to ask: If you're not sure about exactly what the Set contains, why are you storing it in a set in the first place?

Consider instead something like Map&lt;KEY_TYPE, List&lt;? extends IDescriptor&gt;&gt; descriptors; where KEY_TYPE can be a Class&lt;? extends IDescriptor&gt;, a String, or something else that can tell you what type it is.

When adding items to this map you could use:

List&lt;? extends IDescriptor&gt; list = descriptors.get(descriptor.getClass());
if (list == null) {
    list = new ArrayList&lt;&gt;();
    descriptors.put(descriptor.getClass(), list);
}
list.add(descriptor);


And then all you would need to do is to use descriptors.get(VideoDescriptor.class) (assuming VideoDescriptor is not subclassed, otherwise you could loop through the supertypes as well when adding to the list).



Another thought is that you can use a enum to describe what kind of IDescriptor it is, and the IDescriptor type can have a getDescriptorType method. Then you could check if (fragmentDescriptor.getDescriptorType() == DescriptorType.VIDEO)

As I only see a portion of your code and not the big picture, I can't really tell what would be best for you.