0

I have this Java Object which is used to generate Datacenter Id and Datacenter name.

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;
}

The question is how I can get datacenter name using datacenter key. Similar to hashmap I want to get value based on key.

2
  • 2
    Why aren't you just using a hashmap then? Commented Dec 30, 2012 at 19:43
  • 2
    Please follow the Java naming conventions and have class names starting witch a capital letter for better readability of your code. Commented Dec 30, 2012 at 19:43

1 Answer 1

2

Well, if your key is componentstatsid, then just store the retrieved listDatacentersObj objects in a HashMap as in:

// 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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

You can still get the list by using map.getValues()
I there other more simple solution?
it's as simple as it can get. You want reference by some key in constant time - use hash map.
There was a good example with iterator but the author remove it. Could someone again show the solution with Iterator?
@PeterPenzov the removed answer was to create a List like in the code you've shown in your question and when you want a listDatacentersObj with a specific id, iterate over that list's elements and compare each element id to the searched id. It's certainly less efficient in average and worst case (O(n)) and arguably too verbose (5-6 lines of code). Generally it's not good code, which is probably the reason the answer was deleted in the first place (the answerer should get +1 for being honest).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.