title: Why is Java automatically casting from String to Integer when reading LinkedHashMap
body: <p>My Java application is displaying some odd behaviour, and I'm having trouble finding a solution.</p>
  
  <p>This code snippet takes the contents of a <code>LinkedHashMap</code> where the keys and values are both of type <code>String</code> and writes them to a text file. <code>configWriter</code> is of type <code>PrintWriter</code>.</p>
  
  <pre><code>for (Map.Entry&lt;String, String&gt; entry : configMap.entireSet()) {
  
      configWriter.println(entry.getKey() + "=" + entry.getValue());
  }
  </code></pre>
  
  <p>The map was declared:</p>
  
  <pre><code>LinkedHashMap&lt;String, String&gt; configMap = new LinkedHashMap&lt;String, String&gt;();
  </code></pre>
  
  <p>It is populated by reading tokens from a text file using the <code>Scanner</code> class.</p>
  
  <p>One of the values in the map is a <code>String</code> that contains only numbers (an integer), but when the loop gets to this value, it throws a <code>ClassCastException</code> saying that you cannot cast from <code>Integer</code> to <code>String</code>. This makes perfect sense, but the type of the values is String.</p>
  
  <p>Is there a way to force Java to keep this value as a <code>String</code>?</p>