title: How to get element from Java Object
body: <p>I have this Java Object which is used to generate Datacenter Id and Datacenter name.</p>
  
  <pre><code>private List&lt;listDatacentersObj&gt; listDatacenters;
  
  public void initListDatacenters() throws SQLException {
      // Generate List of Datacenters
      listDatacenters = new ArrayList&lt;&gt;();
  
      if (ds == null) {
          throw new SQLException("Can't get data source");
      }
      /* Initialize a connection to Oracle */
      Connection conn = ds.getConnection();
  
      if (conn == null) {
          throw new SQLException("Can't get database connection");
      }
      /* With SQL statement get all settings and values */
      PreparedStatement ps = conn.prepareStatement("select y.componentstatsid, y.name "
              + "FROM component x, componentstats y where x.componentstatsid = y.componentstatsid and y.componenttypeid = 1000");
  
      try {
          //get data from database
          ResultSet result = ps.executeQuery();
          while (result.next()) {
              /* Put the the data from Oracle into Hash Map */
              listDatacenters.add(new listDatacentersObj(result.getInt("COMPONENTSTATSID"), result.getString("NAME")));
  
          }
      } finally {
          ps.close();
          conn.close();
      }
  }
  
  public class listDatacentersObj {
  
      private int datacenterid;
      private String datacentername;
  
      public listDatacentersObj(int datacenterid, String datacentername){
  
          this.datacenterid = datacenterid;
          this.datacentername = datacentername;
  
      }
  
      public int getDatacenterid() {
          return datacenterid;
      }
  
      public void setDatacenterid(int datacenterid) {
          this.datacenterid = datacenterid;
      }
  
      public String getDatacentername() {
          return datacentername;
      }
  
      public void setDatacentername(String datacentername) {
          this.datacentername = datacentername;
      }
              @Override
      public String toString()
      {
          return datacentername;
      }
  
  }
  
  // Get the list with Datacenters
  public List&lt;listDatacentersObj&gt; getlistDatacenters() throws SQLException {
      // Get the list of Datacenters from Oracle
  
      return listDatacenters;
  }
  </code></pre>
  
  <p>The question is how I can get datacenter name using datacenter key. Similar to hashmap I want to get value based on key. </p>