Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > query

Reply
Thread Tools

query

 
 
parag mohite
Guest
Posts: n/a
 
      08-30-2010
how do we convert an ArrayList into a hashMap ??
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-30-2010
On 8/30/2010 7:10 AM, parag mohite wrote:
> 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);

--
Eric Sosman
lid
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      08-30-2010
parag mohite wrote:
>> how do we convert an ArrayList into a hashMap [sic] ?? [sic]


Eric Sosman wrote:
> 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.

--
Lew
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      08-30-2010
On 8/30/2010 4:44 AM, Lew wrote:
> parag mohite wrote:
>>> how do we convert an ArrayList into a hashMap [sic] ?? [sic]

>
> Eric Sosman wrote:
>> 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.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      08-31-2010
>> parag mohite wrote:
>>>> 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.

 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      08-31-2010
On Mon, 30 Aug 2010, Eric Sosman wrote:

> On 8/30/2010 7:10 AM, parag mohite wrote:
>> 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.


Civil unions?

tom

--
It's odd to discover your quips in other people's .sig files. --
Benjamin Rosenbaum
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-31-2010
parag mohite wrote:
>>> how do we convert an ArrayList into a hashMap ??

>


Eric Sosman wrote:
>> * *The same way you convert a roomful of single men into
>> married couples: You add a suitable amount of the missing
>> ingredient.

>


Tom Anderson wrote:
> Civil unions?
>


As opposed to marriages, which often are not very civil.

--
Lew
Very happily married,
by a civil ceremony, as it happens.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-01-2010
On 8/31/2010 8:57 AM, Tom Anderson wrote:
> On Mon, 30 Aug 2010, Eric Sosman wrote:
>
>> On 8/30/2010 7:10 AM, parag mohite wrote:
>>> 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.

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

--
Eric Sosman
lid
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-01-2010
On Mon, 30 Aug 2010 04:10:27 -0700 (PDT), parag mohite
<> wrote, quoted or indirectly quoted someone
who said :

>how do we convert an ArrayList into a hashMap ??


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.
--
Roedy Green Canadian Mind Products
http://mindprod.com

You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      09-01-2010
On Tue, 31 Aug 2010, Eric Sosman wrote:

> On 8/31/2010 8:57 AM, Tom Anderson wrote:
>> On Mon, 30 Aug 2010, Eric Sosman wrote:
>>
>>> On 8/30/2010 7:10 AM, parag mohite wrote:
>>>> 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.

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


I'll have to remember that one for my next code review.

tom

--
MADSKILLZ!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET won't retrieve query results that depend on union query Eric Nelson ASP .Net 5 02-04-2009 10:51 PM
Trying to query the Address table data of AdventureWorks database from Query Analyzer - need help! Learner ASP .Net 1 01-30-2006 08:58 PM
Build dynamic sql query for JSTL <sql:query> Anonymous Java 0 10-13-2005 10:01 PM
xpath query query David Gordon XML 2 05-18-2005 03:33 PM
CAML Query: Multiple Query Fields Issue Jon F. ASP .Net Web Services 0 05-12-2004 08:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57