query

E

Eric Sosman

how do we convert an ArrayList into a hashMap ??

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.

Perhaps you meant a HashSet instead of a HashMap? That
would make more sense, because a HashSet -- any Set -- is a
collection of individual objects, not a collection of pairwise
associations between objects. If so, a simple way is

List<Thing> theList = ...;
Set<Thing> theSet = new HashSet<Thing>(theList);
 
L

Lew

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.

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.
 
D

Daniel Pitts

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.

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.
The third common option is to have a map where the key is a component of
the value:

for (Thing thing: theList) {
thingsByName.put(thing.getName(), thing);
}

I use this frequently for building indexes.
 
M

markspace

parag said:
how do we convert an ArrayList into a hashMap [sic] ?? [sic]


I think another possibility is that the OP wants a HashSet instead of a
HashMap...

Set hash = new HashSet( arrayList );

(Generics should be used here, of course.) But we'll never get
resolution without the OP's clarification.
 
E

Eric Sosman

Civil unions?

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.
 
T

Tom Anderson

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.

tom
 
R

Roedy Green

ITYM: construct a HashMap and feed the pairs one at a time
to its .put() method.

Quite right. put creates the internal Pair objects used to track key
and value, but it is not itself a constructor.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top