Object with in an Object

Hey Guys I am in middle of a situation here...............I have a firm object which I have stored in a HashMap and then I have a firm1 object which is stored as a Vector......Now I want to store this Vector in the HashMap.......does it make sense? is it possible? do reply
[280 byte] By [saranatora] at [2007-10-3 2:13:38]
# 1
You can add a Vector to a HashMap as a key (which would not make sense, but is legal) or as a value). You post is very hard to follow though. I don't know what you're really trying to do.
jverda at 2007-7-14 19:12:29 > top of Java-index,Java Essentials,New To Java...
# 2
An object is an object. If you want to add a Vector to a HashMap go for it. don't know why you'd want to....but you can.
Norweeda at 2007-7-14 19:12:29 > top of Java-index,Java Essentials,New To Java...
# 3

public static void main(String[] args)

{

HashMap hm = new HashMap();

Vector v1 = new Vector();

Vector v2 = new Vector();

v1.add( 0, "Mike" );

v1.add( 1, "Joe" );

v2.add( 0, "Billy" );

v2.add( 1, "Frank" );

hm.put( "Family", v1 );

hm.put( "Friends", v2 );

Vector friends = (Vector) hm.get( "Friends" );

System.out.println( "My second friend is: " + friends.get( 1 ) );

}

Norweeda at 2007-7-14 19:12:29 > top of Java-index,Java Essentials,New To Java...
# 4

> >v1.add( 0, "Mike" );

>v1.add( 1, "Joe" );

> v2.add( 0, "Billy" );

> v2.add( 1, "Frank" );

>}

You don't need to specify the index:

v1.add("Mike");

v1.add("Joe");

v2.add("Billy");

v2.add("Frank");

On my last project, we had HashMaps with ArrayList values (and ArrayLists with HashMap values) all over the place. A pain in the neck to debug--we desperately needed real Java classes for our objects, rather than storing everything in HashMaps/ArrayLists.

MLRona at 2007-7-14 19:12:29 > top of Java-index,Java Essentials,New To Java...
# 5
i know I was not very clear........but yeah I want to add the vector as a Value to the HashMap.
saranatora at 2007-7-14 19:12:29 > top of Java-index,Java Essentials,New To Java...
# 6

> An object is an object. If you want to add a Vector

> to a HashMap go for it. don't know why you'd want

> to....but you can.

tons of reasons to do that.

Returning collections of collections of data is quite common.

Here's a nice method signature for you to ponder over for example:

public static Map<Long, List><Map><String, ?>>> translateTickerIntervalData(

Map<Long, List><IntervalDataInt>> tickerIntervalData)

Darn forum software keeps messing up the nested generics ;(

jwentinga at 2007-7-14 19:12:29 > top of Java-index,Java Essentials,New To Java...
# 7

> > An object is an object. If you want to add a

> Vector

> > to a HashMap go for it. don't know why you'd want

> > to....but you can.

>

> tons of reasons to do that.

> Returning collections of collections of data is quite

> common.

The problem is not "collections of data", that's just fine. But using a HashMap as an alternative to creating a dedicated class sounds ... fishy at best. Every single time I've seen it here I've thought that it would be much more readable with a custom class to hold the data instead of "Vectors in HashMaps".

JoachimSauera at 2007-7-14 19:12:29 > top of Java-index,Java Essentials,New To Java...
# 8

> > > An object is an object. If you want to add a

> > Vector

> > > to a HashMap go for it. don't know why you'd

> want

> > > to....but you can.

> >

> > tons of reasons to do that.

> > Returning collections of collections of data is

> quite

> > common.

>

> The problem is not "collections of data", that's just

> fine. But using a HashMap as an alternative to

> creating a dedicated class sounds ... fishy at best.

> Every single time I've seen it here I've thought that

> it would be much more readable with a custom class to

> hold the data instead of "Vectors in HashMaps".

Possibly, but sometimes it's inevitable.

Say you're writing a request mapper, with multiple request parameters of unknown key and value.

Store the parameternames as keys and the values as Lists of Strings in a HashMap and let the recipient worry about what it all means.

jwentinga at 2007-7-14 19:12:29 > top of Java-index,Java Essentials,New To Java...