Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first example workds becase you are using raw type that is not recomended. </p> <p>To solve your compilation error you might want to not limit the class into T but allow somethi g super the T</p> <p><code>public &lt;T&gt; T getValueFromMap(Map&lt;String, T&gt; map, String key, Class&lt;? super T&gt; valueClass)</code></p> <p>But then you will have not save convertion and you will need to cast result of new instance into (T). And you can not use List as the class because is an iterface and you can not create a instace of it. </p> <pre><code>public &lt;K,V&gt; V getValueFromMap(Map&lt;K, V&gt; map, K key, Class&lt;? super V&gt; valueClass){ if(map.containsKey(key) == false) { try { map.put(key, (V) valueClass.newInstance()); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return map.get(key); } </code></pre> <hr> <pre><code>Map&lt;String,ArrayList&lt;String&gt;&gt; myMap = new HashMap&lt;String, ArrayList&lt;String&gt;&gt;(); List&lt;String&gt; value = getValueFromMap(myMap, "aKey", ArrayList.class); //will work </code></pre> <p><strong>EDIT</strong></p> <p>But what about the base form <code>Map&lt;String,List&lt;String&gt; myMap</code>, that OP expect ?</p> <p>As we should know <code>ArrayList</code> is super type of <code>List</code>, but <code>ArrayList&lt;String&gt;</code> is not super type of <code>List&lt;String&gt;</code> but it is assignable.</p> <p>So lets try it out: </p> <p>Example 1: <code>getValueFromMap(myMap, "aKey", ArrayList.class); //Compilation error</code> </p> <p>Example 2: <code>getValueFromMap(myMap, "aKey", List.class); //Runtime error</code></p> <p>Solution for this might be some assumption that if class is List then create ArrayList. </p> <p>This force us to create some <em>not decent code</em>. </p> <pre><code>public static &lt;V&gt; V createInstace(Class&lt;V&gt; valueClass) throws InstantiationException, IllegalAccessException { if(List.class.isAssignableFrom(valueClass)) { return (V) new ArrayList&lt;V&gt;(); } return valueClass.newInstance(); } </code></pre> <p>This code do not really have anything common with generics but is working. </p> <p>At the end we finish with </p> <pre><code>public static void main(String[] args) { Map&lt;String,List&lt;String&gt;&gt; myMap = new HashMap&lt;String, List&lt;String&gt;&gt;(); List&lt;String&gt; value = getValueFromMap(myMap, "aKey", List.class); //does not work value.add("Success"); System.out.println(value); } public static &lt;K,V&gt; V getValueFromMap(Map&lt;K, V&gt; map, K key, Class&lt;? super V&gt; valueClass){ if(map.containsKey(key) == false) { try { map.put(key, (V) createInstace(valueClass)); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return map.get(key); } public static &lt;V&gt; V createInstace(Class&lt;V&gt; valueClass) throws InstantiationException, IllegalAccessException { if(List.class.isAssignableFrom(valueClass)) { return (V) new ArrayList&lt;V&gt;(); } return valueClass.newInstance(); } </code></pre> <p>And the result if this is <code>[Sucess]</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload