1

a big doubt! I read that HashMap takes references of objects and doesn't copy values to store data. So if I have something like

    HashMap<Integer, Double> map = HashMap<Integer, Double>();
    for(int i = 0; i < 100; i++ ) {
       Integer key = Integer(i);
       Double value = Double(i*2.0);
       map.put(key, value);
    }

What is the result of

    map.get(10);

? Keys and values are created within the for loop so I suppose that they are deleted at the end of for statement and put(10) give something like null. But I think it's a very annoying behaviour because I can't fill a HashMap with a straighforward for loop... I'm wrong?

3
  • "so I suppose that they are deleted at the end of for statement" - no, they are not. They are no longer accessible via i identifier, but they are not deleted. Commented Sep 7, 2014 at 20:39
  • The values created in the loop are not destroyed at the end. This is a garbage collected language; objects are recycled when there are no more references to them, and not before. Commented Sep 7, 2014 at 20:39
  • @Grzegorz Oledzki So I have some confusion about scope in java... When objects are deleted because they go out of scope? Commented Sep 7, 2014 at 20:41

3 Answers 3

3

I'm assuming you meant to ask what the result of map.get(10) is, since map.put(10) is not valid. map.get(10) would return the Double whose value is 20.0.

Since the map variable is declared outside the for loop, the values you put in the map remain there after the end of the for loop. The keys and values are not deleted at the end of the for loop because the map variable holds references to them.

Sign up to request clarification or add additional context in comments.

3 Comments

... would return a reference to a Double ... ?
@AlanStokes I don't understand your question.
I was proposing a clarification.
0

First of all, in Java, all objects are accessed by reference - there is nothing special about the HashMap here.

Second, all objects are kept in memory until the Garbage collector (GC) decides to destroy them. The GC may destroy objects as soon as they are no longer referenced anywhere - it may not destroy them if they are still referenced.

In your example :

  • After the loop, the object referenced by map holds references to the same objects that were referenced by keyand value : those objects cannot be destroyed.
  • If, at a later point, you no longer hold any reference to the HashMap object, it may be destroyed by the GC, and the GC may also decide to destroy the key and the value, if they were only referenced by the Hashmap. Note that there is no guarantee given as to when garbage collection will occur: it won't necessarily happen immediately.
  • You can't do map.put(10), because HashMap doesn't have a method with a compatible signature. The only put method that exists for HashMap expects two parameters.

edit: Since the op corrected map.put(10) to map.get(10): it will return a reference to the same object that was briefly referenced by the variable value during the "i=10" iteration of the loop.

1 Comment

Yes, I edited the put that was wrong. Thanks very clear!
0

The key are hold by refrences as (almost)all objects in Java; java won't delete an object which is strongly referenced by another object.

object created in the loop will only be collected if no other object references them after the loop

since you put them into an hashmap, java will keep them around

note also that put(10, ...) actually is put(new Integer(10), ...) thanks to autoboxing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.