EzDevInfo.com

hashmap interview questions

Top hashmap frequently asked interview questions

How to turn a Ruby Hash into HTTP Params

That is pretty easy with a plain hash like

{:a => "a", :b => "b"}

which would translate into

"a=a&b=b"

but what do you do with something more complex like

{:a => "a", :b => ["c", "d", "e"]}

which should translate in

"a=a&b[0]=c&b[1]=d&b[2]=e"

or even worse, with something like:

{:a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}]

Thanks for the much appreciated help with that!


Source: (StackOverflow)

Differences between HashMap and Hashtable?

What are the differences between a HashMap and a Hashtable in Java?

Which is more efficient for non-threaded applications?


Source: (StackOverflow)

Advertisements

Iterate through a HashMap [duplicate]

Possible Duplicate:
How do I iterate over each Entry in a Collection Map?

What's the best way to iterate over the items in a HashMap?


Source: (StackOverflow)

Java Hashmap: How to get key from value?

If I have the value "foo", and a HashMap<String> ftw for which ftw.containsValue("foo") returns true, how can I get the corresponding key? Do I have to loop through the hashmap? What is the best way to do that?


Source: (StackOverflow)

How to update a value, given a key in a java hashmap?

Suppose we have a HashMap<String, Integer> in Java.

How do I update (increment) the integer-value of the string-key for each existence of the string I find?

One could remove and reenter the pair, but overhead would be a concern.
Another way would be to just put the new pair and the old one would be replaced.

In the latter case, what happens if there is a hashcode collision with a new key I am trying to insert? The correct behavior for a hashtable would be to assign a different place for it, or make a list out of it in the current bucket.


Source: (StackOverflow)

How to understand Locality Sensitive Hashing?

I noticed that LSH seems a good way to find similar items with high-dimension properties.

After reading the paper http://www.slaney.org/malcolm/yahoo/Slaney2008-LSHTutorial.pdf, I'm still confused with those formulas.

Does anyone know a blog or article that explains that the easy way?


Source: (StackOverflow)

What is the significance of load factor in HashMap?

HashMap has two important properties: size and load factor. I went through the Java documentation and it says 0.75f is the initial load factor. But I can't find the actual use of it. Can someone describe what are the different scenarios where we need to set load factor and what are some sample ideal values for different cases?


Source: (StackOverflow)

How to convert a ruby hash object to JSON?

How to convert a ruby hash object to JSON? So I am trying this example below & it doesn't work?

I was looking at the RubyDoc and obviously Hash object doesn't have a to_json method. But I am reading on blogs that Rails supports active_record.to_json and also supports hash#to_json. I can understand ActiveRecord is a Rails object, but Hash is not native to Rails, it's a pure Ruby object. So in Rails you can do a hash.to_json, but not in pure Ruby??

car = {:make => "bmw", :year => "2003"}
car.to_json

Source: (StackOverflow)

How can I convert JSON to a HashMap using Gson?

I'm requesting data from a server which returns data in the JSON format. Casting a HashMap into JSON when making the request wasn't hard at all but the other way seems to be a little tricky. The JSON response looks like this:

{ 
    "header" : { 
        "alerts" : [ 
            {
                "AlertID" : "2",
                "TSExpires" : null,
                "Target" : "1",
                "Text" : "woot",
                "Type" : "1"
            },
            { 
                "AlertID" : "3",
                "TSExpires" : null,
                "Target" : "1",
                "Text" : "woot",
                "Type" : "1"
            }
        ],
        "session" : "0bc8d0835f93ac3ebbf11560b2c5be9a"
    },
    "result" : "4be26bc400d3c"
}

What way would be easiest to access this data? I'm using the GSON module.


Source: (StackOverflow)

Key existence check in HashMap

Is checking for key existence in HashMap always necessary?

I have a HashMap with say a 1000 entries and I am looking at improving the efficiency. If the HashMap is being accessed very frequently, then checking for the key existence at every access will lead to a large overhead. Instead if the key is not present and hence an exception occurs, I can catch the exception. (when I know that this will happen rarely). This will reduce accesses to the HashMap by half.

This might not be a good programming practice, but it will help me reduce the number of accesses. Or am I missing something here?

[Update] I do not have null values in the HashMap.


Source: (StackOverflow)

How to loop through a HashMap in JSP?

How can I loop through a HashMap in JSP?

<%
    HashMap<String, String> countries = MainUtils.getCountries(l);
%>

<select name="country">
    <% 
        // Here I need to loop through countries.
    %>
</select>

Source: (StackOverflow)

Why use symbols as hash keys in Ruby?

A lot of times people use symbols as keys in a Ruby hash.

What's the advantage over using a string?

E.g.:

hash[:name]

vs.

hash['name']

Source: (StackOverflow)

How to create a simple map using JavaScript/JQuery

How can you create the JavaScript/JQuery equivalent of this Java code:

Map map = new HashMap(); //Doesn't not have to be a hash map, any key/value map is fine
map.put(myKey1, myObj1);
map.put(myKey2, myObj2); //Repeat n times

function Object get(k) {
    return map.get(k);
}

Source: (StackOverflow)

How do I copy a hash in Ruby?

I'll admit that I'm a bit of a ruby newbie (writing rake scripts, now). In most languages, copy constructors are easy to find. Half an hour of searching didn't find it in ruby. I want to create a copy of the hash so that I can modify it without affecting the original instance.

Some expected methods that don't work as intended:

h0 = {  "John"=>"Adams","Thomas"=>"Jefferson","Johny"=>"Appleseed"}
h1=Hash.new(h0)
h2=h1.to_hash

In the meantime, I've resorted to this inelegant workaround

def copyhash(inputhash)
  h = Hash.new
  inputhash.each do |pair|
    h.store(pair[0], pair[1])
  end
  return h
end

Source: (StackOverflow)

How does a Java HashMap handle different objects with the same hash code?

As per my understanding I think:

  1. Its perfectly legal for two objects to have same hashcode.
  2. If two objects are equal (using equals ) then they have same hashcode.
  3. If two object are not equal then they cannot have same hashcode

Am I correct?

Now if am correct, I have following question: HashMap internally uses hashcode of the object. Then if two objects can have same hashcode, then how can the HashMap track which key it uses?

Can someone explain how HashMap internally uses the hashcode of the object?


Source: (StackOverflow)