How can I make this faster?
<p>I have made an event system, however, it is too slow.</p>

<p>The issue, is that there are multiple entries in a map that I never actually added. I don't understand how they get there.</p>

<pre><code>public class OrdinalMap&lt;V&gt; {

    private final Map&lt;Integer, V&gt; map;

    public OrdinalMap(Class&lt;? extends Enum&lt;?&gt;&gt; valueType, V virginValue) {
        map = new HashMap&lt;Integer, V&gt;();
        Enum&lt;?&gt;[] enums = valueType.getEnumConstants();
        for (int i = 0; i &lt; enums.length; i++) {
            put(enums[i].ordinal(), virginValue);
        }
    }

    public OrdinalMap(Class&lt;? extends Enum&lt;?&gt;&gt; valueType) {
        this(valueType, null);
    }

    public V put(Integer key, V value) {
        return map.put(key, value);
    }

    public V get(Object o) {
        return map.get(o);
    }

    public Set&lt;Entry&lt;Integer, V&gt;&gt; entrySet() {
        return map.entrySet();
    }

}
</code></pre>

<p>I want to make <em>dispatchEvent</em> faster (less iterations). It has too many iterations because of <em>registerListener</em></p>

<p>There are event handler methods inside of all of the other priorities, when they shouldn't be there. I can't figure out why there are there, but I'm certain it's in <em>registerListener</em>. Because they are inside all priorities, I have to use this check:
    <em>if (mapping.getKey().getAnnotation(EventHandler.class).priority().ordinal() ==     entry.getKey()) {</em></p>

<p>Which makes it even slower.</p>

<pre><code>@Override
public void dispatchEvent(Event event) {
    OrdinalMap&lt;Map&lt;Method, EventListener&gt;&gt; priorityMap = getRegistry().get(event.getClass());

    if (priorityMap != null) {
        CancellableEvent cancellableEvent = null;
        boolean cancellable;
        if (cancellable = event instanceof CancellableEvent) {
            cancellableEvent = (CancellableEvent) event;
            if (cancellableEvent.isCancelled()) return;
        }

        try {
            for (Entry&lt;Integer, Map&lt;Method, EventListener&gt;&gt; entry : priorityMap.entrySet()) {
                for (Entry&lt;Method, EventListener&gt; mapping : entry.getValue().entrySet()) {
                    if (mapping.getKey().getAnnotation(EventHandler.class).priority().ordinal() == entry.getKey()) {
                        mapping.getKey().invoke(mapping.getValue(), event);
                        if (cancellable &amp;&amp; cancellableEvent.isCancelled()) return;
                    }
                }
            }
        } catch (InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void registerListener(EventListener listener) {
    for (Method method : listener.getClass().getMethods()) {
        EventHandler handler = method.getAnnotation(EventHandler.class);
        if (handler != null) {
            Class&lt;?&gt;[] parameters = method.getParameterTypes();
            if (parameters.length == 1) {
                @SuppressWarnings("unchecked")
                Class&lt;? extends Event&gt; event = (Class&lt;? extends Event&gt;) parameters[0];
                EventPriority priority = handler.priority();

                OrdinalMap&lt;Map&lt;Method, EventListener&gt;&gt; priorityMap = getRegistry().get(event);
                if (priorityMap == null) {
                    priorityMap = new OrdinalMap&lt;Map&lt;Method, EventListener&gt;&gt;(EventPriority.class, (Map&lt;Method, EventListener&gt;) new HashMap&lt;Method, EventListener&gt;());
                }

                Map&lt;Method, EventListener&gt; methodMap = priorityMap.get(priority.ordinal());

                methodMap.put(method, listener);
                priorityMap.put(priority.ordinal(), methodMap);

                getRegistry().put(event, priorityMap);
            }
        }
    }
}
</code></pre>
