Returning a HashTable Stored in a HashMap (Only returns Strings?)

D

DaBeef

I wanted a data structure which looked somewhat like this. I did not
wnat to write my own classes, thinking I can use java's Data
Structures. I want my object to look like this
[key1, val1,val2,val3,val4]

I noticed teh Hashmap always returns a String as a value,
concactinating my HashTable into a String such as
val, val2,val3. I do not want to reassemble this and search for
values. I needed a structure which takes 2 dif datas, checks to see
if a contains b
b contains a
check individual elemeents of a against individual elements of b
Can I return a HashTable from a HashMap, or am i wasting my time?
Thank you for your help
 
O

opalpa

HashMap returns Object, not String, the signuatre is
Object get(Object key)
check javadoc
http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html

You can return Hashtable from HashMap. You can return HashMap from
HashMap.
I needed a structure which takes 2 dif datas, checks to see
if a contains b
b contains a

What does it mean for date a to be contained by a date?

Anyway, you're not wasting your time. State what you are doing more
explicitly.

Cheers.
http://www.geocities.com/opalpaweb/
 
D

DaBeef

Can you? Just means two different sets of similar data (Database type
storage). WhenEver I use this code.
Iterator iteratorObject = HashMapObject.keySet().iterator();
while(iteratorObject .hasNext())
{
Object obj = iteratorObject .next();
Object Obj2 = this._HashMapObject.get(obj);
}
but Obj2 is simply a String. FX Rate=1.0000, 3NoHeading=,
Price=1.0000,
With all my values concacted together.
Please elaborate.
Thank-you
 
S

Sam

Your statement: "I noticed teh Hashmap always returns a String as a
value" is not true.

Hashmap is a pair of Objects. String is an Object and most examples
have a String has the key (and possibly as the value) but that is not
necessary.

So you can use a String as the key and a HashMap (or any other type of
Object, including any type of Collection) as the value.

So to put something:

Hashtable ht = new Hashtable();
// code to put stuff in ht
HashMap hm = new Hashmap();
hm.put("key1",ht);

and to get:

Hashtable x = (Hashtable) mh.get("key1");

See http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html
Or http://javaalmanac.com/egs/java.util/coll_CreateMap.html?l=new

Sam
 
D

DaBeef

Thanks for your help, Stupid casting mistake IDK. Stare at teh code
too long you start to lose it. Thanks for your help
 
O

opalpa

It is only a String if you put a String in.

I'm guessing that you don't say
String str = (String) Obj2;
but instead say
System.out.println("contents: "+Obj2);

The latter invokes Obj2.toString() and gives you the concatination.

Cool?

To elaborate even more if you swap the presumed printing statement with
a cast your code will not run anymore as the cast will be invalid. You
can cast to whatever the type is. That is if you put Hashtable
instances into your HashMap then you can
Hashtable current = (Hashtable) Obj2;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top