Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume HashMap and Map are imported from the java.util package. (a) Can a HashMa

ID: 3823902 • Letter: A

Question

Assume HashMap and Map are imported from the java.util package.

(a) Can a HashMap<String,Number> be used in a context where a Map<String,Number> is needed? Give a convincing explanation as to why or why not.

(b) Can a HashMap<String,Number> be used in a context where a HashMap<String,Object> is needed? Give a convincing explanation as to why or why not.

(c) Can a HashMap<String,Number> be used in a context where a HashMap<String, ?> is needed? Give a convincing explanation as to why or why not.

(d) Can a HashMap<String,Number> be used in a context where a HashMap<?,?> is needed? Give a convincing explanation as to why or why not.

Explanation / Answer

[A] Yes , We can use it , because HashMap in child of Map interface.

[B] Yes , becasue Object is Super Class in java. So Number object Value Can be store in Object .

[C] ? is used as a wildcard which is used when providing a type argument, e.g. List<?> foo = ... means that foo refers to a list of some type, but we don't know what. So Yes

[D] Same Reason in C, So Yes

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote