Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
DaBeef - 05 Jan 2006 20:46 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.