query

Discussion in 'Java' started by parag mohite, Aug 30, 2010.

  1. parag mohite

    parag mohite Guest

    how do we convert an ArrayList into a hashMap ??
     
    parag mohite, Aug 30, 2010
    #1
    1. Advertising

  2. parag mohite

    Eric Sosman Guest

    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
     
    Eric Sosman, Aug 30, 2010
    #2
    1. Advertising

  3. parag mohite

    Lew Guest

    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
     
    Lew, Aug 30, 2010
    #3
  4. parag mohite

    Daniel Pitts Guest

    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/>
     
    Daniel Pitts, Aug 30, 2010
    #4
  5. parag mohite

    markspace Guest

    >> 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.
     
    markspace, Aug 31, 2010
    #5
  6. parag mohite

    Tom Anderson Guest

    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
     
    Tom Anderson, Aug 31, 2010
    #6
  7. parag mohite

    Lew Guest

    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.
     
    Lew, Aug 31, 2010
    #7
  8. parag mohite

    Eric Sosman Guest

    [OT] Re: query

    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
     
    Eric Sosman, Sep 1, 2010
    #8
  9. parag mohite

    Roedy Green Guest

    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.
     
    Roedy Green, Sep 1, 2010
    #9
  10. parag mohite

    Tom Anderson Guest

    Re: [OT] Re: query

    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!
     
    Tom Anderson, Sep 1, 2010
    #10
  11. parag mohite

    Arne Vajhøj Guest

    Re: [OT] Re: query

    On 01-09-2010 18:04, Tom Anderson wrote:
    > 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.


    Do you prefer being asked to do a few pirouettes on the ice
    over rewriting the code ?

    :)

    Arne
     
    Arne Vajhøj, Sep 2, 2010
    #11
  12. parag mohite

    Eric Sosman Guest

    Re: [OT] Re: query

    On 9/1/2010 9:23 PM, Arne Vajhøj wrote:
    > On 01-09-2010 18:04, Tom Anderson wrote:
    >> 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.

    >
    > Do you prefer being asked to do a few pirouettes on the ice
    > over rewriting the code ?


    I'm *always* on thin ice in code reviews ...


    --
    Eric Sosman
    lid
     
    Eric Sosman, Sep 2, 2010
    #12
  13. parag mohite

    Eric Sosman Guest

    On 9/1/2010 12:29 PM, Roedy Green wrote:
    > 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.


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

    --
    Eric Sosman
    lid
     
    Eric Sosman, Sep 2, 2010
    #13
  14. parag mohite

    Roedy Green Guest

    On Wed, 01 Sep 2010 21:28:59 -0400, Eric Sosman
    <> wrote, quoted or indirectly quoted
    someone who said :

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


    Quite right. put creates the internal Pair objects used to track key
    and value, but it is not itself a 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.
     
    Roedy Green, Sep 6, 2010
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Learner
    Replies:
    1
    Views:
    1,012
    Marina Levit [MVP]
    Jan 30, 2006
  2. Anonymous
    Replies:
    0
    Views:
    1,532
    Anonymous
    Oct 13, 2005
  3. David Gordon

    xpath query query

    David Gordon, May 18, 2005, in forum: XML
    Replies:
    2
    Views:
    819
    David Gordon
    May 18, 2005
  4. Eric Nelson
    Replies:
    5
    Views:
    1,563
    Alexey Smirnov
    Feb 4, 2009
  5. Jon F.

    CAML Query: Multiple Query Fields Issue

    Jon F., May 12, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    769
    Jon F.
    May 12, 2004
Loading...

Share This Page