Results 1 to 2 of 2
- 01-06-2017, 05:56 AM #1
Member
- Join Date
- Oct 2016
- Posts
- 47
- Rep Power
- 0
- 01-06-2017, 06:14 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Hashmap vs. Map (in terms of their types)
It can be but it doesn't have to. HashMap implements the Map interface and it is usually best practice to assign the implementation to the
interface type. Exceptions are when the implementation provides additional methods one wants to use. The idea is that it makes writing code easier. If you start assigning everything to a HashMap type and decide you want to use a TreeMap, you need to change all HashMap types to TreeMap types. If you simply use the Map reference, you probably need to change it in fewer places.
In a related topic, it is best to pass the interface type to a method rather than the implementation.
Regards,
JimLast edited by jim829; 01-06-2017 at 06:16 AM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
How do raw types of generic types show pregeneric behavior?
By Zarah in forum New To JavaReplies: 1Last Post: 05-12-2016, 10:24 AM -
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 08:17 PM -
Java terms
By Shaolin in forum New To JavaReplies: 2Last Post: 12-27-2007, 06:02 PM
Bookmarks