P
parag mohite
how do we convert an ArrayList into a hashMap ??
how do we convert an ArrayList into a hashMap ??
parag said:how do we convert an ArrayList into a hashMap [sic] ?? [sic]
Eric said:The same way you convert a roomful of single men into
married couples: You add a suitable amount of the missing
ingredient.
A HashMap -- any kind of Map -- contains *pairs* of keys
and values. An ArrayList -- any kind of List -- contains
individual items. You can't make a sensible key/value pair
out of just one item from a List, so you need to add some
additional information from somewhere else. Well, you *could*
make a sort of degenerate Map in which the key and value in
each pair are the same object
List<Thing> theList = ...;
Map<Thing,Thing> theMap = new HashMap<Thing,Thing>();
for (Thing thing : theList)
theMap.put(thing, thing);
... but that's sort of like making all those single men "married"
by having them go off individually and do something in private.
The third common option is to have a map where the key is a component ofparag said:how do we convert an ArrayList into a hashMap [sic] ?? [sic]
Eric said:The same way you convert a roomful of single men into
married couples: You add a suitable amount of the missing
ingredient.
A HashMap -- any kind of Map -- contains *pairs* of keys
and values. An ArrayList -- any kind of List -- contains
individual items. You can't make a sensible key/value pair
out of just one item from a List, so you need to add some
additional information from somewhere else. Well, you *could*
make a sort of degenerate Map in which the key and value in
each pair are the same object
List<Thing> theList = ...;
Map<Thing,Thing> theMap = new HashMap<Thing,Thing>();
for (Thing thing : theList)
theMap.put(thing, thing);
... but that's sort of like making all those single men "married"
by having them go off individually and do something in private.
There's another degenerate Map that fits the original question as stated:
Map <Object, List <Thing>> theMap
= new HashMap <Object, List <Thing>> ();
theMap.put( "Key", theList );
or perhaps the List is the key and the other object is the value.
In this business the "obvious" interpretation of a vaguely-stated
requirement might not be the correct one. It's best to get the
requirements nailed down unambiguously.
parag said:how do we convert an ArrayList into a hashMap [sic] ?? [sic]
The same way you convert a roomful of single men into
married couples: You add a suitable amount of the missing
ingredient.
Tom said:Civil unions?
Civil unions?
how do we convert an ArrayList into a hashMap ??
Yes, I'm from Massachusetts, and yes, I realized that the metaphor
was fraying around the edges. But it was too good to pass up: Just as
in figure skating, artistic merit outpoints technical accuracy.
I'll have to remember that one for my next code review.
Do you prefer being asked to do a few pirouettes on the ice
over rewriting the code ?
see http://mindprod.com/jgloss/hashmap.html#INITIALISING
http://mindprod.com/jgloss/arraylist.html
You have to extract your objects from the ArrayList and feed them
object and key one at a time to the HashMap constructor.
ITYM: construct a HashMap and feed the pairs one at a time
to its .put() method.
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.