ListMultimap does not keep order of insertion

Mary :

I'm currently programming a simulation of an elevator. So I needed to create a ListMultiMap "passengerFloorMap" to store following values:

  • id of passenger
  • startFloor of passenger
  • destinationFloor of passenger

Now I needed to shuffle the keys of "passengerFloorMap". This needed to be done, to create a random order of passengers arriving. For example: First there was "passengerFloorMap" {1=[2,4], 1=[3,4], 1=[1,0], ...}

Then just the keys were shuffled, because I wanted to pick the associated values later:

`private List<Integer> shuffleKeyList(){
        keySet = new ArrayList<>(passengerFloorMap.keySet());
        Collections.shuffle(keySet); 
        return keySet;
 }
` Return example: [12, 130, 15, ...]

Afterwards I created another function to create the new MultiMapList "randomOrder". There the new order of Keys ([12, 130, 15, ...]) should be stored. Also I wanted to get the associated values to these keys from "passangerFloorMap". So the result should look like this: randomOrder = {12=[3,1], 130=[0,4], 15=[2,1], ...}. To make this happen I stored the shuffled list into an ArrayList:

List<Integer> shuffledList = new ArrayList<>();

private List<Integer> shuffleKeyList(){
    List<Integer> keySet = new ArrayList<>(passengerFloorMap.keySet());
    Collections.shuffle(keySet); 
    return keySet;
}

private void pickRandomPassanger(){
    ListMultimap<Integer, Integer> randomOrder = ArrayListMultimap.create(); 
    ArrayList<Integer> list = new ArrayList<>();
    shuffledList = shuffleKeyList();

    for (int i = 0; i < shuffledList.size(); i++){
        liste.add(shuffledList.get(i));
    }
}

Now I want to fill "randomOrder" with the keys (in the new order after shuffle) and the associated values. For this I created a for-loop (in pickRandomPassenger): 3 is just an example, would be replaced by right value

for(int j = 0; j < liste.size(); j++){
    randomOrder.put(liste.get(j), 3);
}

So now the problem arrives. If I call randomOrder.put(liste.get(j), x) it retrieves different value than calling randomOrder.put(liste.get(0), x). Also if j = 0. Here is my output:

keySet = [46, 39, 81, 38, 34, 87, 20, 71, 32,...]
list = [46, 39, 81, 38, 34, 87, 20, 71, 32,...]
randomOrder = {1=[3], 2=[3], 3=[3], 4=[3], 5=[3], 6=[3], 7=[3], 8=[3], 9=[3], ...]

But, if I call

randomOrder.put(list.get(0), 3) 

it retrieves:

randomOrder = {46=[3]}

I found out that the problem should something be caused by the memory-reference. But I don't know how to solve this problem. Hopefully someone of you could help.

GreyFairer :

I guess what you want to say is that you expected the first element to be 46=[3], not 1=[3]?

You got the wrong class for that. ArrayListMultimap only guarantees to keep the insertion order for elements within a key, e.g. if you insert (46, 4) followed by (46, 3), you would get {46=[4, 3]}. But if you insert with different keys, it uses HashMap to determine that order.

If you want keep insertion order of keys, you need a LinkedListMultimap.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=317913&siteId=1