-4

I am trying to create a permutation of a hash map which keys the keys and shuffles them in a random order multiple times but keeps the same object.

So far i have:

Map<Integer, GeoPoint> mapPoints = new HashMap<Integer, GeoPoint>();
ArrayList<Integer> keys2 = new ArrayList<Integer>(mapPoints.keySet());

for (int t =0; t < 50; t ++){

            Collections.shuffle(keys2);

        }

But from what i can gather it is not shuffling them. Can anyone see what im doing wrong.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
MurphyApps
  • 13
  • 2
  • 11

1 Answers1

2

What does "shuffled" look like to you? There's no order for keys in HashMap. You need a LinkedHashMap to preserve insertion order.

Shuffling the Collection of keys won't affect the Map per se; you iterate over it to access the Map keys.

See if this gives you a different ordering after you run it.

Map<Integer, GeoPoint> mapPoints = new HashMap<Integer, GeoPoint>();
System.out.println("before shuffle ");
Set<Integer> keys = mapPoints.keySet();
for (int key : keys) {
    System.out.println("key : " + key + " value: " + mapPoints.get(key));
}
Collections.shuffle(keys);  // don't know why multiple shuffles are required.  deck of cards?
System.out.println("after shuffle ");
for (int key : keys) {
    System.out.println("key : " + key + " value: " + mapPoints.get(key));
}
duffymo
  • 305,152
  • 44
  • 369
  • 561