• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How to create a hash map from an array?

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one probably is tougher.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tougher than what?
"Create a hash map from an array" is an underspecified problem. Which elements of the array are keys, and which are values?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, a HashMap represents a set of key/value pairs. Given a key object, you want to be able to look up a corresponding value object. An array is basically a list of objects. (Or primitives.) The question is, how do the objects in the array relate to each other? Are they keys? Values? Perhaps all the even-numbered index positions in the array correspond to keys, and the following odd positions correspond to the values tha keys should be mapped to?
Basically, there's no one way to do this, and it really depends on the situation. What do the objects in the array represent? What does the Map represent? (Is there even any relationship \i{to} be mapped here?) Perhaps you could show an example of an array you're thinking of, and describe how the HashMap should relate to it.
[ August 14, 2002: Message edited by: Jim Yingst ]
 
Holmes Wong
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since a hash map stores key/value pairs, every mapping needs one and one value. But a hash map also allows null keys and null values. This ia what makes it confusing: we may be able to create a hash map from an array: for example, if this array contains integers, we may use them as keys, and use null values for every mapping.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Until you can specify the algorithm for turning your array into a set of keys matched to a set of values, you're not going to be able to write any code.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Holmes Wong:
for example, if this array contains integers, we may use them as keys, and use null values for every mapping.


Why in the world would you waste cpu time doing that?
 
Holmes Wong
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy:
It is an interview question I got from others.
They told me there is an answer. I didn't see one at my first look, I am still thinking about it since "no answer" is incorrect. I am looking for jobs, have to hack these things no matter how weird they are......
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It isn't a sensible question. As I said before, it's an underspecified problem. You need to know more about the contents of the array and how they are supposed to be used in a hash map.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One could argue that in the context of an interview this is a good "talking points" question with no concise answer (a la "what's the volatile keyword do?") which forces the interview into a discussion of language concepts rather than a repetition of canned answers.
The candidate response could range across:
1) A quizzical look that says "What's a hash map?"
2) "You don't. Next question please"
3) A discussion of why this is an odd question, and an opportunity to talk about what the candidate understands
I have, as things happen, converted an array to a hashmap in the past. Not of primitive types, which is rather silly as Cindy pointed out, but of complex objects that possessed a logical 'primary key'. The array elements became hash-mapped entries with their 'primary key' value as the hash map key (thus making lookup by that primary key easy and efficient).
hth,
bear
[ August 15, 2002: Message edited by: Bear Bibeault ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They told me there is an answer.
Possibilities:
  • They lied.
  • They're on crack.
  • The objective is actually to see how does the candidate react to an underspecified problem. The best repsonse is probably to ask intelligent follow-up questions to determine what it is they really want, and avoid mentioning the previous two possibilities to their faces.


  • [ August 15, 2002: Message edited by: Jim Yingst ]
     
    Forget Steve. Look at this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic