• Post Reply Bookmark Topic Watch Topic
  • New Topic

memory allocation for hashmap and arraylist  RSS feed

 
Deep Saxena
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I suppose hashmap is better in performance when compared to an arraylist. But can anyone let me know which occupies more memory - hashmap or an arraylist ?

Thanks,
Deep
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I suppose hashmap is better in performance when compared to an arraylist.

What makes you suppose that? Performance doing what?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two are totally different. No comparison. Both give very fast lookup with their get() methods. But they are used for completely different tasks. It is rather like asking which is faster, a bicycle or a sailing boat. No comparison, unless you are used to cycling across the sea!
Don't know which uses more memory; I suspect a Map because it maintains two references for each Entry.
 
Anup Srivastava
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList, vector and all map allocates a default initial memory of 10 objects.
In case of ArrayList, when a request of 11th object comes it increases to 1.5 times of present size ie 10 --> 15 --> 22...
In case of Vector, when a request of 11th object comes it increases to 3 times of present size ie 10 --> 20 --> 40...
In case of Map, when it is .75 (equal to load factor) full object comes it increases to 2.0 times of present size ie 10 --> 15 --> 22...


NOTE :- in case of Vector instead of incretement to double you can define increment size. then it will increase only that no of objects only not Double.

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you reopened such an old thread?

Maps usually have capacities which are exact powers of 2 (I think): default size = 16: when you pass the 75% load factor it enlarges to 32. That makes it easier to use the hashcode.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!