如何从Java对象中获取元素
我有一个Java对象,用于生成数据中心Id和数据中心名称如何从Java对象中获取元素,java,Java,我有一个Java对象,用于生成数据中心Id和数据中心名称 private List<listDatacentersObj> listDatacenters; public void initListDatacenters() throws SQLException { // Generate List of Datacenters listDatacenters = new ArrayList<>(); if (ds == null) {
private List<listDatacentersObj> listDatacenters;
public void initListDatacenters() throws SQLException {
// Generate List of Datacenters
listDatacenters = new ArrayList<>();
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<listDatacentersObj> getlistDatacenters() throws SQLException {
// Get the list of Datacenters from Oracle
return listDatacenters;
}
问题是如何使用数据中心密钥获取数据中心名称。与hashmap类似,我希望基于键获取值 如果您的键是componentstatsid,那么只需将检索到的listDatacentersObj对象存储在HashMap中,如下所示:
那么为什么不使用hashmap呢?请遵循Java命名约定,让类名以大写字母开头,以提高代码的可读性。您仍然可以使用map.getValues获得列表还有其他更简单的解决方案吗?它尽可能简单。您希望在常量时间使用哈希映射中通过某个键进行引用。有一个迭代器的好例子,但作者删除了它。有人能用迭代器再次显示解决方案吗?@PeterPenzov删除的答案是创建一个列表,就像您在问题中显示的代码一样,当您需要一个具有特定id的listDatacentersObj时,迭代该列表的元素,并将每个元素id与搜索到的id进行比较。在平均和最坏的情况下,在5-6行代码上肯定效率较低,而且可能过于冗长。一般来说,这不是一个好代码,这可能是答案被删除的原因,因为回答者诚实,应该得到+1。
// private List<listDatacentersObj> listDatacenters; use the below map instead of this list
private Map<Integer, listDatacentersObj> listDatacenters =
new HashMap<Integer, listDatacentersObj>();
...
public void initListDatacenters() throws SQLException {
...
try {
//get data from database
ResultSet result = ps.executeQuery();
while (result.next()) {
/* Put the the data from Oracle into Hash Map */
final int id = result.getInt("COMPONENTSTATSID");
listDatacenters.put(id, new listDatacentersObj(id, result.getString("NAME")));
}
} finally {
ps.close();
conn.close();
}
}
}