title: Java Hashmap to Object while running
body: <p>Is there any way to cast the value of a Java HashMap to an Object at runtime like this:</p>
  
  <pre><code>claas Foo {
      public int a;
      public String b;
  }
  Foo foo = new Foo();
  
  HashMap&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
  
  for(Map.Entry&lt;Foo, Bar&gt; entry : map.entrySet()) {
    String propertyName foo = entry.getKey();
    Object o = entry.getValue();
  
    foo[propertyName] = o; // Does not wokring
  }
  </code></pre>
  
  <p>I tryed to parse a SQL-Query result to a object. Now i've written my own Seralize-Algorithm. Bot it doesn't work. Is there a better way to Unerialize Object from the Database?</p>
  
  <pre><code>Connection connection = DriverManager.getConnection(instance);
  PreparedStatement statment = connection.prepareStatement("SELECT * FROM Foo;");
  
  Foo data;
  
  ResultSet rs = statment.executeQuery(query);
      if (rs != null &amp;&amp; rs.next()) {
          for (Field field : data.getClass().getDeclaredFields()) {
          try {
              if ((String.class.equals(field.getType()))) {
                  field.set(String.class, rs.getString(field.getName()));
              } else if ((Boolean.class.equals(field.getType()))) {
                  field.set(Boolean.class, rs.getBoolean(field.getName()));
              } else if ((Integer.class.equals(field.getType()))) {
                  field.set(Integer.class, rs.getInt(field.getName()));
              }
  
          } catch (IllegalAccessException e) {
          } catch (IllegalArgumentException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>