title: how to store data with generic types in an Java arraylist
body: <p>I will try my best to explain my problem and hopefully someone can help me.</p>
  
  <p>Pair class:</p>
  
  <pre><code>public class Pair {
     String key;
     Class&lt;?&gt; value;
     public Pair(String key, Class&lt;?&gt; value){
        this.key = key;
        this.value = value;
     };
     // you have the setter and getter methods
  }
  </code></pre>
  
  <p>Pairs class:</p>
  
  <pre><code>public class Pairs {
     Pair[] paris = new Pair[0];
     // you have the setter and getter methods
     public void addPair(Pair pair) {
        // assume it will add a pair to the array
     }
  }
  </code></pre>
  
  <p>Problem: I need to load data from a database table. The column types are different here. There are BOOLEAN, VARCHAR, DATE and others. So I need to read and store the data with corresponding java type into the Pair object. How do you convert from generic type to String or Boolean? And how do you do the other way around?</p>
  
  <p>I found an answer for converting generic type to String:</p>
  
  <pre><code>Class&lt;?&gt; value = getValue();
  if (value.isInstance(String.class))
  String newValue = (String)(Object) value; // is it correct?
  </code></pre>
  
  <p>Then how can I convert a String to Class&lt; ?> and store the data into the arraylist? Because I want to create:</p>
  
  <pre><code>Pair pair = new Pair("name", value); // but value can be String, Integer, or Boolean
  </code></pre>
  
  <p>Thanks.</p>