6

In some cases i suffered the operation at that time,

We can use the both HashMap or Model Class (POJO) as a ArrayList Generic Like ArrayList<HashMap<K,V>>() OR ArrayList<ModelName>().

Can you suggest me which is the better as a memory point of view and performances vise.

I know both are the better in it place, but i just want to know if i have option to take both alternative.... which will the better ?

e.g. Suppose i have 2 variable both are string in POJO class, and same both are for HashMap.

so we have the list objects Like..

  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 object of ABC class,

And in HashMap with List object it will...

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 object of HashMap.

Now tell me which will i have to use for better performance? Which is occupier of high memory ?

GovindRathod
  • 867
  • 1
  • 8
  • 22
  • It depends on your use case. Please explain the concrete task – AlexR Aug 07 '14 at 08:15
  • 1
    http://yojava.wordpress.com/2011/06/17/why-i-prefer-pojos-to-maps-for-domain-objects/ – user3145373 ツ Aug 07 '14 at 08:15
  • 1
    You question is unclear. The HashMap and and POJO are not comparable. Elaborate the question with your use case. – Santosh Aug 07 '14 at 08:15
  • 1
    I think you are referring to a Java Bean more than to a POJO – Pablo Lozano Aug 07 '14 at 08:16
  • 1
    If you worried about comparative performance of POJO/Bean vs Map than most likely you are thinking about the wrong things. Their only common attribute is that they allow to store and access a number of property/value pairs. They store these "pairs" differently and therefore the memory consumption and access performance will differ. You should read about (Hash)Map and how it works internally. Then you may realise that Map performance will depend on the actual Keys you put into the Map... – Germann Arlington Aug 07 '14 at 08:27
  • @AlexR please see edited , and i said first that if i have case in which i can use both so what should do...? – GovindRathod Aug 07 '14 at 09:03
  • @Santosh here i am not comparing both just asking that in certain condition if i have both option so what should do ...? – GovindRathod Aug 07 '14 at 09:04
  • @Pablo please provide me some good references for that if you have,it would be appreciated.. – GovindRathod Aug 07 '14 at 09:05
  • @Concentrated_Attitude You can find them in SO: http://stackoverflow.com/questions/1394265/what-is-the-difference-between-a-javabean-and-a-pojo – Pablo Lozano Aug 07 '14 at 09:13

2 Answers2

5

In general, you use a Collection (List, Map, Set) to store objects with similar characteristics, that's why generics exist. So using a Map to store objects of several classes forces you to cast every object you have stored when you are getting it, which is not the cleanest code we can write. Also, even using a HashMap, where the objects are easily fetched using the hashCode value, the access is slower than to a Java Bean attribute... and you have always to ensure that those objects have a good hashCode() generator. But anyway, I cannot imagine when this could be a bottleneck in performance in a regular application.

So my recommendation is using a Java Bean every time that you know in compilation time how many attributes you are going to store and their classes, using the Map to store elements of the same class.

For example, if you have a DB table with users, and you want to "cache" the users to access to anyone of them quickly, you could do something like this:

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

   //setters and getters
}

And then store them in a Map to have a fast access to them by their id:

Map<Long,User> usersMap= new HashMap<>();
...
usersMap.put(user.getId(),user);
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
  • What about, instead of multiple primitive properties (like we're used to seeing in POJO's), you only have a single property which is a map of maps of maps of lists? – cs_pupil May 14 '19 at 22:12
  • I think this answers my question: https://softwareengineering.stackexchange.com/a/130000 – cs_pupil May 14 '19 at 22:45
0

I would assume that fast lookup is not your use-case because the way you are using Map you can't lookup by name in O(1).

Also, it is waste of space as you are creating 1 new map for each name=value pair entry to be stored.

I would rather suggest that if you have some unique key available (say integer Id) then store your structure in a HashMap like this:

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

Now this can be used for fast lookup as well as stores all attributes of your Model. And you can always access all your Model object using m.values(); method.

sactiw
  • 21,935
  • 4
  • 41
  • 28