This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Go into your Java installation file, and explore it until you find a file called src.zip. Unzip that file, go into the "java" then the "util" folders and find "HashSet.java." Open it. You will find that HashSet uses a HashMap, so you open HashMap, and there you find that HashMap uses an array of Entries. I haven't read it properly, but shall leave that to you. It appears to double its capacity whenever the array is more than 75% full, and to create a new Entry array whenever its capacity changes/
Joined: Jul 27, 2006
Generally, memory doesn't get expanded, it gets reallocated and copied. The choice about when to do that is hard to make, so the standard collections do it for us, choosing sensible times. For example, an empty ArrayList may have a 100 element array in it, and when you fill that, it'll allocate a 200 element array, and copy all the data across.
I don't know the exact strategy used, and I don't care, but it will be a variant on the above.