This discussion is archived
3 Replies Latest reply: Jan 11, 2013 6:06 AM by PhHein RSS

HashMap will grow and shrink dynamically

800839 Newbie
Currently Being Moderated
Hi,

By default Hashmap takes 16 as an initial capacity. If it is more than 16 it will grow dynamically based on the constant factor.
If it is less than 16 elements set whether it will shrink dynamically as well? Please clarify.

Thanks.
  • 1. Re: HashMap will grow and shrink dynamically
    gimbal2 Guru
    Currently Being Moderated
    797836 wrote:
    Hi,

    By default Hashmap takes 16 as an initial capacity. If it is more than 16 it will grow dynamically based on the constant factor.
    If it is less than 16 elements set whether it will shrink dynamically as well? Please clarify.
    If it would do that, wouldn't it be pointless to set an initial capacity?
  • 2. Re: HashMap will grow and shrink dynamically
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    since the API does not mention shrinking I'd say it will not shrink automatically.

    But This is just a guess, so try it...

    bye
    TPD
  • 3. Re: HashMap will grow and shrink dynamically
    PhHein Guru Moderator
    Currently Being Moderated
    797836 wrote:
    Hi,

    By default Hashmap takes 16 as an initial capacity. If it is more than 16 it will grow dynamically based on the constant factor.
    If it is less than 16 elements set whether it will shrink dynamically as well? Please clarify.

    Thanks.
    The size() call will give you a reduced number of entries. Entries are stored internally in an array, that will grow, but never shrink. I.e. if you add 25 entries to a new HashMap it will have a capacity of 32 and a size of 25. After removing 10 of those entries capacity will still be 32, but size will be 15.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points