<p>In my spring application context file, I have something like:</p>
<pre><code>&lt;util:map id=&quot;someMap&quot; map-class=&quot;java.util.HashMap&quot; key-type=&quot;java.lang.String&quot; value-type=&quot;java.lang.String&quot;&gt;
    &lt;entry key=&quot;some_key&quot; value=&quot;some value&quot; /&gt;
    &lt;entry key=&quot;some_key_2&quot; value=&quot;some value&quot; /&gt;   
&lt;/util:map&gt;
</code></pre>
<p>In java class, the implementation looks like:</p>
<pre><code>private Map&lt;String, String&gt; someMap = new HashMap&lt;String, String&gt;();
someMap = (HashMap&lt;String, String&gt;)getApplicationContext().getBean(&quot;someMap&quot;);
</code></pre>
<p>In Eclipse, I see a warning that says:</p>
<p><strong>Type safety: Unchecked cast from Object to HashMap&lt;String,String&gt;</strong></p>
<p>What went wrong?</p>

Well, first of all, you're wasting memory with the new
```
HashMap
```
creation call. Your second line completely disregards the reference to this created hashmap, making it then available to the garbage collector. So, don't do that, use:
```
```
private Map<String, String> someMap = (HashMap<String, String>)getApplicationContext().getBean("someMap");
```
```
Secondly, the compiler is complaining that you cast the object to a
```
HashMap
```
without checking if it is a
```
HashMap
```
. But, even if you were to do:
```
```
if(getApplicationContext().getBean("someMap") instanceof HashMap) {
    private Map<String, String> someMap = (HashMap<String, String>)getApplicationContext().getBean("someMap");
}
```
```
You would probably still get this warning. The problem is,
```
getBean
```
returns
```
Object
```
, so it is unknown what the type is. Converting it to
```
HashMap
```
directly would not cause the problem with the second case (and perhaps there would not be a warning in the first case, I'm not sure how pedantic the Java compiler is with warnings for Java 5). However, you are converting it to a
```
HashMap<String, String>
```
.
HashMaps are really maps that take an object as a key and have an object as a value,
```
HashMap<Object, Object>
```
if you will. Thus, there is no guarantee that when you get your bean that it can be represented as a
```
HashMap<String, String>
```
because you could have
```
HashMap<Date, Calendar>
```
because the non-generic representation that is returned can have any objects.
If the code compiles, and you can execute
```
String value = map.get("thisString");
```
without any errors, don't worry about this warning. But if the map isn't completely of string keys to string values, you will get a
```
ClassCastException
```
at runtime, because the generics cannot block this from happening in this case.