Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Priority Queue for HashMap Object in Java

I have created a HashMap object which stores a String as key and corresponding value as int. Now I want to have a Priority Queue which have all the String present in HashMap object with value as reference for assigning priorities. I have written the following code

public class URIQueue {

      private HashMap<String,Integer> CopyQURI;
      private PriorityQueue<String> QURI;

      public class TComparator<String> {
         public int compareTo(String s1, String s2) {
            if (CopyQURI.get(s2) - CopyQURI.get(s1) >= 0) {
               return 1;
            } else {
               return 0;
            }
         }
      }

      public URIQueue() {
         CopyQURI=new HashMap<>(100);
         TComparator<String> tc=new TComparator<>();
         QURI=new PriorityQueue<>(100, tc); //Line x
      }
 }

Line x is showing error cannot infer type argument for priority queue. Please guide me what mistake I have done.

like image 689
Prannoy Mittal Avatar asked Sep 24 '12 23:09

Prannoy Mittal


People also ask

Is HashMap a priority queue?

The purpose of Priority Queues is to be able to access the element of highest priority the quickest and the purpose of the HashMap is to allow you to map keys to values and so therefore access those elements in O(1) Time Complexity.

How do I create a priority queue for an object?

To create a priority queue, use one of the constructors. We can optionally pass the Comparator instance for custom ordering of the items. Notice the sequence of items in the priority queue is not always in sorted order, but when we retrieved the items then items are retrieved always in sorted order.

How do I create a priority queue object in Java?

PriorityQueue(): Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering. PriorityQueue<E> pq = new PriorityQueue<E>(); 2. PriorityQueue(Collection<E> c): Creates a PriorityQueue containing the elements in the specified collection.

Are maps a priority queue?

A priority queue is a concept like a list or a map; just as a list can be implemented with a linked list or with an array, a priority queue can be implemented with a heap or with a variety of other methods such as an unordered array.


1 Answers

The error you are referring to states, that it cannot guess the generic type parameter which you have omitted. The reason for that is that the constructor you are using is not known. It is not known, because you second argument is not a comparator. Your comparator has to implement the java.util.Comparator interface in order to be type safe for the constructor to accept.

public class TComparator<String> implements Comparator<String> {

   @Override
   public int compare(String arg0, String arg1) {
      // ...
   }
}

Also mind, in the Comparator interface the appropriate method is called compare and not compareTo.

A general advice, I have to agree with Louis Wasserman, for two given arguments a comparator should always return the same result and not depend on the state of the application. It's just too easy not to think of some case and the application is eventually flawed.

like image 141
Konrad Reiche Avatar answered Oct 12 '22 17:10

Konrad Reiche