I need to create priority set/array that bases on:
public interface IListener { public Priority getPriority(); public enum Priority { HIGHEST, HIGH, NORMAL, LOW, LOWEST; } } IListeners are stored in:
HashMap<Class<? extends IListener>, Set<IListener>> listeners = new HashMap<>(); I am looking to make method that will always add IListener in 1st place after its Priority group. Example: Given Set contains some IListeners with this order.
{ HIGHEST, HIGHEST, HIGH, HIGH, LOW, LOW, LOW, LOWEST }
Adding listener with Priority == HIGH would result in:
{ HIGHEST, HIGHEST, HIGH, HIGH, HIGH, LOW, LOW, LOW, LOWEST }
Bold one being newly added.
I know I could just iterate and add at 1st "free slot", but question is rather - does Java provide some good-looking (maybe better?) solutions? Might be just for future reference.
getPriority().ordinal()for getting the numeric value that corresponds to the priority order (order in which the constants are declared).PriorityQueuesolve your problem? Use this constructor:PriorityQueue(Comparator<? super E> comparator).LinkedHashSet. ALinkedHashSetkeeps the items in insertion order, whereas you want priority order.PriorityQueueis that ties are broken arbitrarily, whereas you want ties to be resolved on a first-in-first-out basis (I think). I think aMap<Priority, Deque<IListener>>would meet your exact requirements but I don't know if there's a simpler way.