Reputation: 547
What is the difference between using a List<Map<String, String>>
vs List<Object>
in terms of usage and performance.
Suppose I have to create a list of map having only 3 types of key value pair, I can instead create an Object having only 3 properties and make a list of objects.
The question here is out of the two approaches which one should be used in which scenario?
Upvotes: 12
Views: 6209
Reputation: 140633
Lets first clarify what we are talking about: it is
List<Map<String, String>>
versus
List<Foo>
where Foo objects have a number of "properties", that would be stored in a map (as key value pairs) with option one.
In that case, you absolutely go for option two. Simply because good OOP is about creating helpful abstractions, aka models. Meaning: when you have attributes that belong together, then using a class to wrap around them is the natural way to go.
That gives you slight advantages on performance (because you avoid the map access), but the core thing is: it allows you to write compile time checked code. You see:
int foo = list.get(0).map.get("key");
can fail at runtime - you don't know if map contains that key, and if that is an Integer.
But
int foo = list.get(0).getFoo(); // resp. ...get(0).fieldName
can be checked by the compiler! (yes, those get() calls can still fail at runtime, but that is something you don't get around anyway)
Beyond that: do not worry about performance on this level: first of all, you have to understand what really affects your performance when writing Java code. Because you absolutely want to avoid that "this code is ugly but might give better performance" thoughts sneak into your design. Focus on writing clean code that gets the job done in a straight forward way. Do never write less expressive code because you assume it is faster. When you have a performance problem - profile your code, identify the root cause and fix that.
But do not allow for premature optimization thoughts to impact the quality of your code.
Upvotes: 27
Reputation: 44250
Provided your class looks something like this:
class Foo
{
private final String a;
private final String b;
private final String c;
// constructor and getters
}
Using a List<Foo>
would result in:
Performance
Performance of using a class is likely very similar to a map. Most map implementations, HashMap
for example, provide O(1) get and put operations. It's not going to be significantly slower than calling a getter method for your object.
Usage, if the 3 "keys" are always the same:
The class is likely much easier to use. If you have a Map<String, String>
there's no easy way to enforce that each map contains exactly 3 keys "A", "B" and "C". What if someone adds key "D"? What if someone removes key A?
With a custom class, this is easy to enforce through the constructor - a compile-time check.
Usage, if the 3 "keys" are not the same:
Without having multiple classes, and possibly some kind of class hierarchy, you can't easily accomplish this. A map is probably better.
Upvotes: 4