title: Java How to: Creation of Generic List?
body: <p>I need to create <code>generic list</code> that can act as <code>List&lt;ClassA&gt;</code> or <code>List&lt;ClassB&gt;</code> because I have following situation. </p>
  
  <pre><code>if(e instanceof A)
  return A;
  else
  return B
  </code></pre>
  
  <p>But I want to populate one <code>List Result</code> which can act as either <code>List&lt;ClassA&gt; Result</code> or <code>List&lt;ClassB&gt; Result</code>, am sure we can use generics here but i am not sure how to use it?</p>
  
  <p><strong>Update</strong></p>
  
  <p>All i want to do is that runtime, my List should be populated with proper type, either <code>class A or class B</code>, </p>
  
  <pre><code>if()
  {
      //data is present in List&lt;ClassA&gt; classAList = new ArrayList&lt;ClassA&gt;();
      //return classAList
  }
  else
  {
    //return List&lt;ClassB&gt; classBList = new ArrayList&lt;ClassB&gt;();
  }
  </code></pre>
  
  <p>Hope this helps. </p>