Why does EnumMap<T>.keySet() return a Set<T>() and not an EnumSet<T>()?
<p>I'm writing a program with a lot of enumerations, and I'm having to return the keyset of an EnumMap a lot. But <code>EnumMap.keySet()</code> returns a <code>Set()</code>, so in order to get the <code>EnumSet</code> I want, I have to use a cast:</p>

<pre><code>EnumMap&lt;SomeEnum, Object&gt; myMap = getMap();
EnumSet&lt;SomeEnum&gt; myEnum = (EnumSet&lt;SomeEnum&gt;) myMap.keySet();
</code></pre>

<p>If I don't cast, the compiler will complain of a type mismatch; it cannot convert from <code>Set&lt;SomeEnum&gt;</code> to <code>EnumSet&lt;SomeEnum&gt;</code>. It seems unnecessary to have to cast this, as the keys of an EnumMap will always be an enumeration. Does anyone know why the <code>keySet()</code> method was constructed this way? I've thought at times it might have something to do with EnumSet being an abstract class, but surely <code>EnumMap</code> could just return whatever the factory method of <code>EnumSet</code> provides.</p>

<p>Cheers, all!</p>

<p>EDIT: I'm very sorry, the above code throws a <code>CastClassException</code>. You could get the EnumSet by using</p>

<pre><code>EnumSet&lt;SomeEnum&gt; myEnum = EnumSet.copyOf(myMap.keySet());
</code></pre>

<p>I really should have checked before posting.</p>
