Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it Possible to create a Queue for HashMap set?

Right now I am trying to create a producer/consumer thread, the producer thread goes through all possible combinations of letters and creates their respective MD5 hashes. Then each combination and its hash is put into the HashMap<String,String>. Now in my consumer thread I want to be able to use the Queue<> collection on the hashmap so my consumer thread may call poll() etc thus removing values atc like a Queue but still giving me the capability of seeing both the combination and its hash when calling poll() How would I go about doing this? I have the HashMap but dont know how to 'make' or cast it as a Queue. Thanks.

like image 767
David Kroukamp Avatar asked Jun 19 '12 18:06

David Kroukamp


People also ask

Is HashMap a queue?

Hi, LinkedHashMap is not thread-safe and it's not of type Queue.

Which is faster set or HashMap?

HashMap is faster/ than HashSet because values are associated with a unique key. HashSet is slower than HashMap because the member object is used for calculating hashcode value, which can be same for two objects. Only one object is created during the add operation.

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.

Are Hashmaps faster than ArrayList?

The ArrayList has O(n) performance for every search, so for n searches its performance is O(n^2). The HashMap has O(1) performance for every search (on average), so for n searches its performance will be O(n). While the HashMap will be slower at first and take more memory, it will be faster for large values of n.


3 Answers

The LinkedHashMap type is like a combination of a HashMap and a Queue - it stores key/value pairs, but also remembers the order in which they were inserted. This might be exactly the type you're looking for. There is no explicit poll() function, but if you get an iterator over the LinkedHashMap you will visit the elements in the order in which they were added. You could probably then write a function like this:

public <KeyType, ValueType> KeyType first(LinkedHashMap<KeyType, ValueType> map) {
    assert !map.isEmpty();
    return map.iterator().next();
}

which will give you back the first element. Just make sure to synchronize appropriately.

Alternatively, you could consider just storing key/value pairs inside a Queue by defining a helper class Pair and then storing Pairs in the queue.

Hope this helps!

like image 132
templatetypedef Avatar answered Oct 26 '22 07:10

templatetypedef


You should not use a HashMap without handling the thread-safety of your code. Else, you may end with a Live-lock.

To be able to iterate your Map with the order in which keys were inserted, you can use a LinkedHashMap.

Map m = Collections.synchronizedMap(new LinkedHashMap(...));

The producer would push entries like this (nothing special):

m.put(key, object)

The consumer would poll entries like this:

while (someCondition) {
    Map.Entry nextEntry = null;

    // This block is equivalent to polling
    {
         synchronized(s) {
             Iterator i = s.iterator(); // Must be in the synchronized block
             if (i.hasNext()) {
                 nextEntry  = i.next();
                 i.remove();
             }
         }
    }

    if (nextEntry != null) {
         // Process the entry
         ...
    } else {
         // Sleep for some time
         ...
    }
    // process
}
like image 39
blacelle Avatar answered Oct 26 '22 09:10

blacelle


I suggest you create a Queue of EntrySet -

Queue<EntrySet<String,String>> queue = new SynchronousQueue<EntrySet<String,String>>();
for (EntrySet<String,String> entry:map.entrySet()) {
   queue.add(entry);
}

You can consider using another type of queue, which lets you put the elements, and only the prdocuer waits in case of non empty such as LinkedBlockingQueue.
The producer will then be able to recompose a map based on the EntrySet objects, if needed.

like image 22
Yair Zaslavsky Avatar answered Oct 26 '22 09:10

Yair Zaslavsky