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 ) );
}
> >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.
> 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 ;(
> > 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".
> > > 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.