Unchecked cast from Object to List
<p>I am trying to extend a solution to the problem posted <a href="https://stackoverflow.com/questions/26976978/type-safety-unchecked-cast-from-object-to-list">here</a>.</p>

<p>The castList method shared there works greats for non-generic types.</p>

<pre><code>public static &lt;T&gt; List&lt;T&gt; castList(Object obj, Class&lt;T&gt; clazz)
{
    List&lt;T&gt; result = new ArrayList&lt;T&gt;();
    if(obj instanceof List&lt;?&gt;)
    {
        for (Object o : (List&lt;?&gt;) obj)
        {
            result.add(clazz.cast(o));
        }
        return result;
    }
    return null;
}
</code></pre>

<p>The issue we are having is trying to use this same idea when the return type is not something simple like a list of strings, but when the list is itself of another generic, like a list of maps.</p>

<pre><code>List&lt;Map&lt;String, Object&gt;&gt; list = (List&lt;Map&lt;String, Object&gt;&gt;) out.get("o_cursor");
</code></pre>

<p>Trying to use castList as follows produces type mismatch: cannot convert from List&lt;Map&gt; to List&lt;Map&lt;String, Object&gt;&gt;.  Is there a way to extend castList to do this?</p>

<pre><code>List&lt;Map&lt;String, Object&gt;&gt; list = castList(out.get("o_cursor"), Map.class);
</code></pre>

<p>I attempted a modified version specific to lists of maps, but still get an error "The method castMapList(Object, Class&lt;Map&lt;K,V&gt;&gt;) is not applicable for the arguments 
 (Object, Class&lt;Map&gt;)" which seems like a different flavor of the same error.</p>

<pre><code>public static &lt;K, V&gt; List&lt;Map&lt;K, V&gt;&gt; castMapList(Object object, Class&lt;Map&lt;K, V&gt;&gt; clazz) {        
    if (object instanceof List&lt;?&gt;) {
        List &lt;Map&lt;K, V&gt;&gt; result = new ArrayList&lt;Map&lt;K, V&gt;&gt;();

        for (Object o: (List&lt;?&gt;) object) {
            result.add(clazz.cast(o));
        }

        return result;
    }

    return null;
}
</code></pre>