Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two ways, one which avoids the tag completely, the other using a naughty but nice utility method.<BR> The problem is pre-genericised Collections...<BR> I believe the rule of thumb is: "cast objects one thing at a time" - what this means when trying to use raw classes in a genericised world is that because you don't know what is in this Map&lt;?, ?&gt; (and indeed the JVM might even find that it isn't even a Map!), it obvious when you think about it that you can't cast it. If you had a Map&lt;String, ?&gt; map2 then HashSet&lt;String&gt; keys = (HashSet&lt;String&gt;)map2.keySet() does not give you a warning, despite this being an "act of faith" for the compiler (because it might turn out to be a TreeSet)... but it is only a <b><i>single</b></i> act of faith.<BR> <BR> PS to the objection that iterating as in my first way "is boring" and "takes time", the answer is "no pain no gain": a genericised collection is guaranteed to contain Map.Entry&lt;String, String&gt;s, and nothing else. You have to pay for this guarantee. When using generics systematically this payment, beautifully, takes the form of coding compliance, not machine time!<BR> One school of thought might say that you should set Eclipse's settings to make such unchecked casts errors, rather than warnings. In that case you would have to use my first way.</p> <pre><code>package scratchpad; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Vector; public class YellowMouse { // First way Map&lt;String, String&gt; getHashMapStudiouslyAvoidingSuppressTag(HttpSession session) { Map&lt;?, ?&gt; theHash = (Map&lt;?, ?&gt;)session.getAttribute("attributeKey"); Map&lt;String, String&gt; yellowMouse = new HashMap&lt;String, String&gt;(); for( Map.Entry&lt;?, ?&gt; entry : theHash.entrySet() ){ yellowMouse.put( (String)entry.getKey(), (String)entry.getValue() ); } return yellowMouse; } // Second way Map&lt;String, String&gt; getHashMapUsingNaughtyButNiceUtilityMethod(HttpSession session) { return uncheckedCast( session.getAttribute("attributeKey") ); } // NB this is a utility method which should be kept in your utility library. If you do that it will // be the *only* time in your entire life that you will have to use this particular tag!! @SuppressWarnings({ "unchecked" }) public static synchronized &lt;T&gt; T uncheckedCast(Object obj) { return (T) obj; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload