/* * @topic T02635 Java Generics demo * @brief class PriorityQueHash *** WRONG implementation -- use PriorityQueTree instead, because HashMap isn't a sorted container! */ package demo; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.Iterator; import java.util.TreeMap; public class PriorityQueHash< E > { private HashMap< Integer, E > map; public PriorityQueHash() { map = new HashMap< Integer, E >(); } public void push( int priority, E value ) { map.put(priority, value); } public E pull() { // Quite an effort to get to the "top" of the HashMap: Set<Map.Entry< Integer, E > > entries = map.entrySet(); Iterator<Map.Entry< Integer, E >> it = entries.iterator(); Map.Entry< Integer, E > entry = null; if ( it.hasNext() ) { entry = it.next(); it.remove(); } else { return null; } return entry.getValue(); } public int size() { return map.size(); } }//class PriorityQueHash< E >