Reputation: 746
I am new to java so please help and guide
Case 1: if a declare a java map inside a loop.
for (Document doc : docList) {
Map<String, String> input = new HashMap<String, String>();
}
Case 2: if a declare a java map outside a loop.
Map<String, String> input = new HashMap<String, String>();
for (Document doc : docList) {
}
which case is more optimized(best practice) way of declaration and why?
AJ
Upvotes: 0
Views: 3657
Reputation: 12809
The first approach will incur substantial object churn (especially if your collection docList
is large). Java object allocation is a relatively expensive operation overall, and the garbage collection overhead incurred by that object churn will also hit your performance.
The second approach will re-use the same map over and over again (just ensure you don't forget to empty it before each new iteration).
It's almost always better in terms of performance to use the second approach, however the first approach makes it cleaner in your code that you don't intend the Map
to be used outside of the loop (it has a narrower scope), which I sometimes consider a good thing. Essentially, my conclusion is:
Map
's visibility scope, option 2 otherwise, or if you're really concerned about getting the best possible performance.Upvotes: 0
Reputation: 5837
Actually In your case no point of optimization, Because, both are not same. You may want to know which one is better with the following case.
Map<String, String> input = null;
for (Document doc : docList) {
input = new HashMap<String, String>();
}
OR
for (Document doc : docList) {
Map<String, String> input = new HashMap<String, String>();
}
In this case the first one is little optimized because of the declaring them map once but initializing it for every entry in docList.
Upvotes: 0
Reputation: 7902
Well it depends on your usage - If you need new Map
per iteration then initialize in loop else outside of it.
Since you are confuse between these two - I believe you should go with Case 2 - Because it will initialize the Map
only once and if you initialize it in loop then Map
will not be accessible out of the loop.
also I think it will be more helpful if you explain what you are doing with that Map
instance.
Upvotes: 4
Reputation: 308743
The Map goes out of scope when you exit the loop in the second one; it's eligible for GC unless you assign it to something else.
If you intend to use it elsewhere, you'll have to go with #1.
Others have already explained the need for declaring it inside the loop if you need one per iteration. Here's an example:
List<Map> maps = new ArrayList<Map>();
for (int i = 0; i < 10; ++i) {
Map temp = new HashMap();
maps.add(temp);
}
Upvotes: 0
Reputation: 1520
It depends. Depends on what you want to achieve.
for (Document doc : docList) {
Map input = new HashMap();
}
This is useful when for each iteration, you need to have a new map with totally different values.
Map input = new HashMap();
for (Document doc : docList) {
}
This will be useful when you want to use values of previous iterations into the next iterations.
Also in the second case, the map will be available for your use even after the loop. In first, you won't be able to access the map once loop is over,
Upvotes: 3