What types can we assign to K in HashMap<K,V>? Is it only numeric types (int, float) or we can assign user defined objects?
What types can we assign to K in HashMap<K,V> ? Is it only numeric
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use any type as long as it has sane
equals()andhashCode()implementations.Strictly speaking: you can use any reference type, but it won’t work as expected if the type doesn’t have sane implementations of those methods.
Note that you can not use the primitive types (
int,float, …) but can use their wrapper types instead (Integer,Float, …). This is because generics can only handle reference types.