title: Storing database entries in an ArrayMap
body: <p>This is somewhat a 2 question in one. It's for a video game and I am using libgdx, so it must be relatively fast.</p>
  
  <p>I have object that will be read and written to a derby database. Some of those objects data structure will be defined at runtime, so I cannot make a POJO with getters and setters and using the persistance API for example.</p>
  
  <p>Instead a data object will look like this</p>
  
  <pre><code>import java.sql.Types;
  
  import com.badlogic.gdx.utils.Array;
  
  public class Creature extends PBDBsystem_table
  {
      protected static Array&lt;PBDBsystem_field&gt; s_field_list = new Array&lt;PBDBsystem_field&gt;();
      protected static String s_table_name = new String ("creature");
  
      Creature ()
      {
          create ( s_table_name, s_field_list );
      }
  
      public static boolean init()
      {
          s_field_list.add ( new PBDBsystem_field ("pk", Types.INTEGER, 0, "", true, true ));
          s_field_list.add( new PBDBsystem_field ("name", Types.VARCHAR, 30 ));
          s_field_list.add( new PBDBsystem_field ("cost", Types.INTEGER, 0, "1" ));
          s_field_list.add( new PBDBsystem_field ("strength", Types.INTEGER ));
  
          create_table ( s_table_name, s_field_list);
  
          return true;
      }
  }
  </code></pre>
  
  <p>For your information, the full length constructor is defined as:</p>
  
  <pre><code>public PBDBsystem_field ( String name, int type, int size, String default_value, boolean not_null, boolean primary_key )
  </code></pre>
  
  <p>What this basically do is that init() will create a list of field in an Array that will remain statically in the class and a reference to the table name and field list is passed to the parent class though create(). So that each object has a reference on it's data structure (Which is allocated only once). Then the parent class define the data container for each object like this:</p>
  
  <pre><code>private ArrayMap&lt;String, String&gt; p_data_list; /** structure to contain database data */
  </code></pre>
  
  <p>Right now I am placing all fields as strings. The key is the field name, and the value is the field data. I am using generic getters and setters in the parent class that require a key like for example:</p>
  
  <pre><code>public void set ( String key, String value)
      {
          p_data_list.put( key, value);
  
      }
  
      public String get ( String key )
      {
          return p_data_list.get(key);
      }
  </code></pre>
  
  <p><strong>Question 1:</strong> Is it recommended to store everything as string as it will sometimes demand conversion to int which could slow things down. I was thinking of using for example the Interger class instead of primitives and make an array map of Object instead of String. But I should still get conversion and casting issues, but they should remain in the parent class if I do all the generic getters/setter correctly.</p>
  
  <p>Ok, during the creation of the object, I had by reflex created a new string instance for each field in the table like this.</p>
  
  <pre><code>protected void create ( String table_name, Array&lt;PBDBsystem_field&gt; field_list )
      {
          p_table_name = table_name;
          p_field_list = field_list;
  
          p_data_list = new ArrayMap &lt;String, String&gt; ();
  
          p_data_list.ordered = false;
  
          for ( PBDBsystem_field field: p_field_list)
          {
              p_data_list.put ( field.name, new String() );
          }
      }
  </code></pre>
  
  <p>This method reference the table name and field list to the parent class, then for each field it create a new string to contain the data. But if I modify the content of a field using the set method above, it will use another string to insert it in the array map. Which leads me to</p>
  
  <p><strong>Question 2</strong>: Will it do a deep copy into the array map's string, or will it simply reference the string that was passed in the set method? Meaning that there is no reason to actually instantiate a string for each field. I should rather leave everything to null.</p>
  
  <p>Considering it's for a video game, I want to minimize the use of the garbage collector by making reusable object or reduce pointless object destruction.</p>