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
opalpa@gmail.com - 05 Jan 2006 20:57 GMT
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/
DaBeef - 05 Jan 2006 21:06 GMT
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
opalpa@gmail.com - 05 Jan 2006 21:18 GMT
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;
Sam - 05 Jan 2006 21:12 GMT
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
DaBeef - 05 Jan 2006 21:17 GMT
Thanks for your help, Stupid casting mistake IDK. Stare at teh code
too long you start to lose it. Thanks for your help