Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

question about efficiency, should i use hashmap?

843789Aug 25 2009 — edited Aug 27 2009
Hi all
i read a csv file and store each line's data into a class MyData.
this class contains a variable of id - unique for object.
i want to store all my MyData object in a collection for later use.
if i want to access them in the fastest way, should i store my objects in a hashmap, and the key value would be their id?
like -
HashMap hashmap = new HashMap();
MyClass mc1 = new MyClass(1); //id =1
MyClass mc2 = new MyClass(4); //id =4

//i add the objects
hashmap.put(mc1.getID(),mc1);
hashmap,put(mc2.getID(),mc2);

//then when i want to get a certain object, where id is 4:

MyClass mc = hashmap.get(4);
should i use other collection? or is it fine that way?

Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 24 2009
Added on Aug 25 2009
5 comments
188 views