title: Java String replaceAll Recursive behavior
body: <p>Yesterday I was working on customized letters and memos. And I have made mapping keywords, like: [Dateofbirth], [Email], [Employee], [Salary] etc, which will be replace at the time of generation.</p>
  
  <blockquote>
    <p>Example: dear [Employee], your Current Salary is [Salary].</p>
  </blockquote>
  
  <p>expected output:</p>
  
  <blockquote>
    <p>Output: dear John, your Current Salary is 12000.</p>
  </blockquote>
  
  <p>I am using <code>replaceAll()</code> method here is a code.</p>
  
  <pre><code>   String str = "dear [Employee], your Current Salary is [Salary].";
          Map&lt;String,String&gt; vals = new HashMap&lt;String,String&gt;();
          vals.put("[Employee]","John");
          vals.put("[Salary]","12000");
          for(String key:vals.keySet()){
              str=str.replaceAll(key,vals.get(key));
          }
          System.out.println(str);
  </code></pre>
  
  <p>but the out is: </p>
  
  <pre><code>dJohn1200012000 [JohnJohnJohnJohnJohnJohnJohnJohn], JohnJohnu12000 Cu1200012000Johnnt 1200012000John1200012000John is [1200012000John1200012000John].
  </code></pre>
  
  <p>I was confused and Googled it and tried to make it correct, after that I changed <code>replaceAll()</code> with <code>replace()</code></p>
  
  <pre><code>EX: str=str.replace(key,vals.get(key));
  </code></pre>
  
  <p>Now it is working fine. The question is Why replace all is doing such kind of behavior what is the core concept of <code>replaceAll()</code> and When to use <code>replace()</code> when to use <code>replaceAll()</code>.
  Thanks</p>