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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.