• Post Reply Bookmark Topic Watch Topic
  • New Topic

regarding hashmap  RSS feed

 
nikhil govind
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using array for storing datas but gradually the number of data is going on increasing and hence i have to change the array size on a regular basis.
So I want to use hashmap instead for storing the details. The data i pick are from different tables having a unique key value in all the tables thus making
a group of detailed value for a particular element in array.
Kindly help me in this regard.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you need help with? It sounds like you got the right idea already: Make a class which holds the details of related data, and use the HashMap to store instances of that class with the key.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nikhil govind wrote:I am using array for storing datas but gradually the number of data is going on increasing and hence i have to change the array size on a regular basis.
So I want to use hashmap instead for storing the details. The data i pick are from different tables having a unique key value in all the tables thus making
a group of detailed value for a particular element in array.

Sound reasonable to me. However, there are a few things to consider:
1. The closest thing to an array in the Java Collections API is an ArrayList; and it can handle the fact that your data will increase. Using it rather than a HashMap will almost certainly involve the least amount of rewriting, although it may not be as fast. You may even want to consider it as an interim solution that gives you a bit of 'breathing space' to design your next stage properly.
2. An array/ArrayList allows you to hold duplicate elements, a Map doesn't (unless you define it as HashMap<Key, List<Element>> or use a MultiMap).
3. A HashMap takes up considerably more space than an array or ArrayList, and works best if you can estimate (actually, slightly overestimate) the number of keys it will contain.
4. HashMap, in order to work properly, requires that each element has an associated hashCode(). If the type already has this method defined, that should be fine; but it does worry me a bit that these items are coming from more than one source.

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!