Getting all key-value pairs from a hashmap in a random order
Joined: Nov 15, 2009
In my iterator i want
1) every key-value pair is retrieved only once.
2) all key-value pairs are retrieved in one iteration of the hashmap
3)every time i run my iterator i get a different sequence of key-value pairs
James Sabre wrote:One does not need to keep a separate List. Each time just create an ArrayList constructed using the set of (key,value) pairs obtained from Map.entrySet() and then shuffle the ArrayList.
It depends on the map implementation. Maps are allowed to use Map.Entry as a view for the current entry while iterating. I quote from the Javadoc of Map.Entry:
These Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the setValue operation on the map entry.