I have a Hash Table like,

HashTable ht = { (1, 1), (2, 1), (3, 1) }

Now, I implement it like, Integer foo = Integer(1) and declare hash table like,

HashTable ht = { (foo, foo), (2, foo), (3, foo) }

Now, as per I understood from this, it will reduce heap space used by JVM. Is this correct ?

Another point is that, in C, I usually use structure like,

HashTable ht = { (1, mem), (2, mem), (3, mem) }
{ where mem is memory location (say 10) of 1 }

And then use the location to access the value. Now, if mem value is less than Int (say Byte), I can save space. However, I don't understand how to implement this in Java. Or is there any other way to reduce space of the hash table ? (means by reducing repeatedly storing same object in Java's way). Any idea ? Thanks in advance.

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?

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?

Thanks.

"Is this a just-for-interest question, or do yo have an actual application where the memory usage of a HashTable is an issue?" == No. I have faced this issue while Coding. I have original structure like this,
key => value1 => value 2 (HashMultiMap- Key with Multiple Values). And same value, I have to store for both key and value like

key 1 --> values 1 , 2, 3
key 2 ---> values 2, 9, 10

So, I am thinking to reduce space by removing repeated objects example,

key 1 --> values 1 , A, 3
key 2 ---> values A, 9, 10

where A is such a procedure (less than long) that reduces the Space for example As I use in C, Pointer concept.

Actually this is because of Guava's HashMultiMap. It says after some insertions (say for example 2/3 million URL string, in 8GB RAM) that Heap Memory is full. I used some URL part as key and others as value. I don't understand why Heap Space is full (because it should store more according to some result-set. That's why I am searching for some basic easy way to solve Heap Memory issue). Memory Space is also an issue to me because I have to store more key value pairs. Thanks.

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.

commented: 10 +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.