RoDev
RoDev

Reputation: 61

Which java collection shall I use to store my data?

My retrived data from SQL looks like this

ID      Name
1       abc
2       xyz
3       def

Which Java collection variable shall I use to store and retrieve them based on postion alter.

I tried with list...But is was only including ID values,not the Name column.

Any help would be highly Appriciated

Upvotes: 1

Views: 286

Answers (2)

Dhanush Gopinath
Dhanush Gopinath

Reputation: 5749

Like Suresh ATTA said Map is the best solution for this scenario. But in case you are going to get more columns from the DB, then it is better if you write a class around it.

An object of that class will represent a row, and the list of object will represent the result set you queried for.

For your current scenario it looks like:

class Name {
  String ID;
  String Name;
}

List<Name> allNames = // query from DB and resultset mapped to classes properly.

allNames.get(0) will give you the first row, and allNames.get(allNames.size()-1) will give you the last row.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122026

You might want to use Map collection which is useful to store key value pairs where Id is your key and value is your name.

Map<Long,String> map = new HashMap<Long,String>();

Upvotes: 7

Related Questions