I have different classes:
class Gato{...}
class Perro{...}
class Pulpo{...}
I would like to store the different objects in the same container or list to treat them later:
Gato gato = new Gato("Garfield");
Perro perro = new Perro("Tobi");
Pulpo pulpo = new Pulpo("Paul");
But obviously I can't do it in a list tipada
:
ArrayList <Gato> listaContenedor = new ArrayList<Gato>();
listaContenedor.add(gato);
listaContenedor.add(perro); //Perro cannot be converted to Gato
How could I store them in the same container/list?
Don't use
Object
default .If you have any base types that they inherit from, especially if it's an interface , create a list with that base type:
You can make a list with all of them and share behavior:
And if you want to give them common behavior, add the method to the interface:
And access that behavior from your list:
Honestly, if you're using Object as a base class for a container, you should check your domain architecture because it's an indicator that there's something wrong going on.
JSON
To use them
JSON
you will need to put it in the dependencies (objectsJSON
are very common so they are never too much in a project) and import it wherever you use it.You can store them in objects
JSON
. With this you can create different structures that will be better for you in one case or another.JSON
ofJSONs
JSONArray
ofJSONs
...
In this example I see a clearer one
JSON
inJSONs
which theJSON
parent can be treated as the container and create oneJSON
for each object so you can then treat them as different objects.Then to retrieve it:
List <Object>
The simplest but not the most recommended . It can cause problems if you don't know the origin of the list's data, as can be seen in this case . Why does List contains return false? . Or if you intend to expand the logic of the list in the future and add other types of objects, you would have to change all the code of how you treated the list.
The simplest thing would be to put them in a list of
Objetos
:And to collect them by casting them to their respective types:
You can also create the list without "typing":
Doc: java.util.ArrayList
The classic container, which many frameworks call a "context" is a
Map
. In aMap
you can save objects under a search key or tag. For convenience, the tag can be aString
, so aMap
defined asMap<String, Object>
allows you to store any object under a given string:then, I could get them back to Max with
miContenedor.get("mi-perro")
.As with other collections, since the object to be retrieved is a
Object
from the point of view of theMap
, in order to retrieve it as a dog, it must be cast.Map
also provides methods to get:Set
of the map entries (Set<Map.Entry<K,V>>
) that becomes a collection with the pairs key (labels), value (stored object) that are inside the map.Set
the keys (Set<K>
) -labels-Collection<V>
)which allows you to iterate and work with the stored values in different ways depending on what you need to do.
And there are different implementations that I would highlight for you to investigate:
TreeMap
,LinkedHashMap
, andHashMap
.Here is the api documentation .