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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post 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));
		
	}
}


admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Guest

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

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

Guest

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

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

cosmindumy
Posts: 7
Joined: Sun Apr 13, 2014 4:04 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

It is not just the compilation that you need to consider here. It fails at run time.
If you like our products and services, please help us by posting your review here.

ago123
Posts: 2
Joined: Fri Feb 12, 2021 6:53 am
Contact:

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

Post 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 1203 times

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 39 guests