1

I have three Hash Table like that,

HashTable ht1 = { (1, 100), (2, 200) }
HashTable ht2 = { (1, 100), (2, 200) }
HashTable value = { (100, null), (200, null) }

Is it possible in Java to store a pointer in ht1 and ht2 instead of 100 and 200, which points (and can access) to 100 and 200 of value hashtable. Example, I want structure like ht1 = { (1, pointer1), (2, pointer2) } where pointer1 ----) 100 (which is stored on value hashtable) Can anybody help me how can this be possible ? I am using Java inbuilt Hashtable construct. Thanks.

asked Mar 10, 2012 at 22:10
2
  • 2
    Could you explain your question more fully please, or rather what you're trying to achieve. Commented Mar 10, 2012 at 22:14
  • @AlanFoster, I want structure like ht1 = { (1, pointer1), (2, pointer2) } where pointer1 ----) 100 (which is stored on value hashtable). Commented Mar 10, 2012 at 22:19

4 Answers 4

3

A couple of comments. Hashtable is useful when you need to have synchronized access to the elements in a Map, if that's not the case HashMap is preferred.

In Java we don't have "pointers", but surely we have references to objects (please remember that in Java all objects are passed by value, not by reference). And yes, you can store references to objects as values in a Map. I think you're confusing concepts from C/C++ with concepts in Java, maybe you should explain exactly what do you want to do with the "pointers".

Just to be sure - you can have a Map such as this one in Java:

Map<Integer, Integer> table = new HashMap<Integer, Integer>();

In the above code, the keys and values of the Map are references to Integer objects, and the Integer class is immutable, meaning that you can't change its value once it's in place - but of course, you can change the value pointed to by a key in the Map.

EDIT :

The sample in the question would look like this in Java:

Map<Integer, Integer> ht1 = new HashMap<Integer, Integer>();
ht1.put(1, 100);
ht1.put(2, 200);
Map<Integer, Integer> ht2 = new HashMap<Integer, Integer>();
ht2.put(1, 100);
ht2.put(2, 200);
Map<Integer, Integer> value = new HashMap<Integer, Integer>();
value.put(100, null);
value.put(200, null);

In the above code all the integers in the three maps are references to immutable objects: what appears to be the number 100 in reality is a reference to the object new Integer(100), and because Integer is an immutable class, it's possible that all three references to new Integer(100) point to exactly the same object in memory.

So, answering your question: yes it's possible in Java to store a pointer in ht1 and ht2 instead of 100 and 200, which points (and can access) to 100 and 200 of value hashtable. In fact that's what is happening already, and there's no other way to do it - because maps in Java can not store primitive types (like int), only references. And again, given that all instances of Integer are immutable, you can not change their value, because doing so would change the values in the other places where they're being shared and used.

answered Mar 10, 2012 at 22:17
2
  • Thanks. Actually I want to access the stored value in another HashTable by using 'references' and no need to change the 'references'. like ht1 = { (1, pointer1), (2, pointer2) } where pointer1 ----) 100 (which is stored on value hashtable). But, how to get those 'references' ? Commented Mar 10, 2012 at 22:29
  • The question remains unclear, I'm afraid. All values (and all keys) in a Java hashtable are references, and if you have two hashtables, let's say ht1 = {(1,reference1)} and ht2 = {(1,reference2)}, you can reference anything you want from the values, in particular both could point to the same number, like this: reference1 = reference2 = 100; and both references can be used as keys in the value hashtable. Commented Mar 11, 2012 at 2:12
0

Yes. If you use Integer instead, as they are passed by reference into the hashtable;

answered Mar 10, 2012 at 22:14
1
  • Can you kindly illustrate that by using simple example java code or pseudo code. Commented Mar 10, 2012 at 22:16
0

Yes. You need to store it as an Integer instead of primitive int. Note that Integer is immutable so you cannot change it. Changing it leads to a new object and the pointers in ht1 and ht2 still point to the old one.

answered Mar 10, 2012 at 22:14
0

Store an object that wraps the value you want a pointer to in a JavaBean with getters and setters. then you can modify the contents externally. References to the wrapper can be stored and accessed, and will change the contents of the HashMap as well.

answered Mar 10, 2012 at 22:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.