0

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

1
  • 3
    Those are pretty different things--think about it. Commented Jun 13, 2012 at 9:27

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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,

3 Comments

I would like to know which case would take more memory? Case 1, will it create a new map object for every iteration? If yes, then when these objects will be destroyed A. as loop move to next entry OR B. It keeps it in memory and wait for GC
It depends on number of elements you are storing. But yes, in first case, for each iteration, jvm has to create a new HashMap reference, while in second, it will have the same reference. Objects on heap are not destroyed as soon as they gets out of scope. They are destroyed by GC. Their reference gets deleted immediately.
Please visit javaprepare.blogspot.com/2007/08/… to read more about difference between "Objects" and "References"
0

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);
}

Comments

0

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.

Comments

0

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:

  1. If your collection of documents could be large, use option 2.
  2. If your collection of documents are guaranteed to be small, use option 1 if you want your code to be very explicit about the Map's visibility scope, option 2 otherwise, or if you're really concerned about getting the best possible performance.

2 Comments

If we have same key for each iteration Case 1: String key1 = "key1"; String key2 = "key2"; for (Document doc : docList) { Map<String, String> input = new HashMap<String, String>(); input.put(key1, "value1"); input.put(key2, "value2"); dosomthing(); } Case 2: String key1 = "key1"; String key2 = "key2"; Map<String, String> input = new HashMap<String, String>(); for (Document doc : docList) { input.put(key1, "value1"); input.put(key2, "value2"); dosomthing(); }
@user1453247 You'll get best performance by using option 2, and you can even skip cleaning the map up between iterations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.