In Java there are primitives (int, char, float, byte, boolean) and there are Objects.
A HashTable can only contain references to Objects. No primitives and no Objects, just references to Objects. If you try to put an int into a HashTable the compiler automatically creates an Integer Object for you (that's an optimised ptocess b.t.w.) and puts a reference to that Object into the HashTable. If you put the same Object multiple times into a HashTable the table will just contain multiple references to that one Object.
The space taken by a reference is something private to the particular JVM you are running on at the time. There is no reason to think that a reference on a cellphone JVM will be the same size as a reference on a 64 bit server JVM.
Is this a just-for-interest question, or do yo have an actual application where the memory usage of a HashTable is an issue?
JamesCherrill
... trying to help
8,668 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33
OK, so
1. Since the map only contains references, never Objects, your approach is based on a misunderstanding. You cannot store an object in a hashmap. If you try to add the same object twice you will always get two references to the same object.
2. The JVM won't use all available 8Meg by default - you have to play with the memory options on the java or javaw command to allocate very large amounts.
3. If this is really a problem then maybe look at alternative structures such as having a simple array of URLs and using the hashmap to store arrays of ints as its values, where the ints are indexes into the URL array. My guess, however, is that you won't gain much (except complexity).
Remember - don't confuse objects with references in Java.
JamesCherrill
... trying to help
8,668 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33