I am making a View Model in which I plan to receive a JSONObject through Web Services, the issue is that I need to receive an object that is a list of data lists, I have an idea of how to represent this, creating a variable.
private Map<Integer,Map<Integer,Map>> MiListaDeListas;
But while researching I came across the doubt of HashMap , I know that one is an implementation of the other, but I really couldn't understand the concept between them.
What is the difference and in what cases is it better to use one of the other.
Map
is an interface that defines the general behavior of a structure that maintains a relationship ofkey --> value
.So
HashMap
it is just one implementation (although probably the most common) of oneMap
of several that are possible. Other implementations ofMap
are for exampleHashtable
,ConcurrentHashMap
,WeakHashMap
, etc. Each has its pros and cons depending on what you're trying to do.When possible, especially if your code is part of a low-level generic library, it is advantageous to define the code using the
Map
. This allows the same code to be used with any of its implementations according to the user's taste.For example, if you define the following method:
...then the same method can be executed without problems by passing it a
HashMap
,Hashtable
,ConcurrentHashMap
,TreeMap
or any of the implementations ofMap
.But if your code absolutely requires the type of to
Map
be aHashMap
(perhaps for performance reasons or whatever), then it's okay to useHashMap
to prevent other implementations from being used in that case.