Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.488 :

Posted: Mon Jun 04, 2012 7:59 am
by ETS User
Question: Which of these statements concerning maps are true?
Answer: It is permissible for a map to contain itself as a key.
Example code that generates StackOverflowError at runtime...

Soo, what does this question mean???

Code: Select all

import java.util.HashMap;

public class Main {
	public static void main(String[] args) {
		HashMap map = new HashMap();
		map.put(map, "hello"); // 1
		map.put(map, "world"); // 2
		//System.out.println(map.get(map));
		
	}
}


Re: About Question com.enthuware.ets.scjp.v6.2.488 :

Posted: Mon Jun 04, 2012 10:16 am
by admin
Your code is correct. It is not permissible for a map to contain itself as a key. That is why it is an incorrect option.

HTH,
Paul.

Re: About Question com.enthuware.ets.scjp.v6.2.488 :

Posted: Mon Jun 04, 2012 10:44 am
by Guest
admin wrote:Your code is correct. It is not permissible for a map to contain itself as a key. That is why it is an incorrect option.

HTH,
Paul.
Hmm, on Standard Test 2, I saw the above answer was picked as a correct option.

Re: About Question com.enthuware.ets.scjp.v6.2.488 :

Posted: Mon Jun 04, 2012 11:11 am
by Guest
Guest wrote:
admin wrote:Your code is correct. It is not permissible for a map to contain itself as a key. That is why it is an incorrect option.

HTH,
Paul.
Hmm, on Standard Test 2, I saw the above answer was picked as a correct option.
Never mind, my bad.

Re: About Question com.enthuware.ets.scjp.v6.2.488 :

Posted: Mon Apr 14, 2014 11:46 am
by cosmindumy
Is still unclear for me.
Map<Map, String> m=new HashMap<Map, String>();
m.put(m, "this map");
This code compiles fine. Is the question referring to something else?

Re: About Question com.enthuware.ets.scjp.v6.2.488 :

Posted: Mon Apr 14, 2014 11:48 am
by admin
It is not just the compilation that you need to consider here. It fails at run time.

Re: About Question com.enthuware.ets.scjp.v6.2.488 :

Posted: Tue Aug 31, 2021 3:29 am
by ago123
but this codes compiles and run well

Code: Select all

        
        Map<Map, String> m = new HashMap<>();
        m.put(m, "");
        System.out.println(m);
        
Map key.PNG
Map key.PNG (23.74 KiB) Viewed 1947 times

Re: About Question com.enthuware.ets.scjp.v6.2.488 :

Posted: Tue Aug 31, 2021 7:48 pm
by admin
Yes, but even if it compiles and runs, it is not allowed as per the JavaDoc API documentation of Map
Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map.
The run time behavior has changed in the recent versions of Java but the restriction is still there in the API.