What is better HashMap vs POJO (Getter Setter, Model Class) in Java and Android

On some occasions I underwent surgery at this time,

We can use both HashMap or Model (POJO) classes as ArrayList Generic Like generator ArrayList<HashMap<K,V>>() OR ArrayList<ModelName>()

.

Can you tell me what is better than memory point and performance.

I know both are better at this place, but I just want to know if I have the opportunity to take both alternatives .... which is better?

eg. Suppose I have 2 variables which are strings in POJO class and the same as for HashMap.

so we have objects of the Like list ..

  final  ArrayList<Abc> listAbc = new ArrayList<Abc>();

 for(final int i=0;i<2;i++){
    Abc modelAbc = new Abc();
    abc.setName("name");
    abc.setLastName("lastName");
    listAbc.add(modelAbc);
  }

      

in this case I have to take two objects of class ABC,

And in a HashMap with a list, the object will be ...

final ArrayList<HashMap<String,String>> listMap = new ArrayList<HashMap<String,String>>();

for(final int i=0;i<2;i++){
 HashMap<String,String> hashAbc = new HashMap<String,String>();
 hashAbc.put("NAME","firstName");
 hashAbc.put("LASTNAME","lastName");
 listMap.add(hashAbc);
}

      

In this case, I have to use two HashMap objects.

Now tell me what do I need to use for better performance? What does high memory take up?

+5


source to share


2 answers


As a rule, you use a collection of ( List

, Map

, Set

) for storing objects with similar characteristics, so there are generics. Therefore, using Map

multiple classes to store objects forces you to cast every object you stored when you get it, which is not the cleanest code we can write. Also, even using HashMap

where objects are easily fetched using value hashCode

, access is slower than Java Bean attribute ... and you should always make sure these objects have a good generator hashCode()

. But anyway, I can't imagine when this could be a performance bottleneck in a typical application.

Therefore, I recommend using Java Bean every time you know at compile time how many attributes you are going to store and their classes, using Map to store elements of the same class.

For example, if you have a database table with users, and you want to "cache" users for quick access to any of them, you can do something like this:



class User {
    long id;
    String name;
    ... //other attributes

   //setters and getters
}

      

And then save them to the card so you can quickly access them by their ID:

Map<Long,User> usersMap= new HashMap<>();
...
usersMap.put(user.getId(),user);

      

+4


source


I would suggest that quick search is not your use case, because when using Map, you cannot search by name in O (1).

Also, it is a waste of space as you create 1 new map for each name = value pair entry that will be saved.

I would prefer that if you had some kind of unique key (say, an integer ID), then store your structure in a HashMap like this:



Map<Integer, Model> m = new HashMap<Integer, Model>();

      

This can now be used for quick searches as well as storing all the attributes of your model. And you can always access all of your Model objects using m.values ​​(); method.

0


source







All Articles