Best structure for list of key-value (integer, string) to be shuffled

后端 未结 5 2129
抹茶落季
抹茶落季 2021-02-06 03:37

I need to implement a structure in Java that is a key-value list (of types Integer-String) and I want to shuffle it.

Basically, I would like to do something like that.

5条回答
  •  故里飘歌
    2021-02-06 04:11

    Create a Pair class, that holds both the Integer and the String and then add multiple Pair objects to a List, which will be shuffled.

    public class Pair {
      private Integer integer;
    
      private String string;
    
      //accessors
    }
    

    Then:

    List list = new ArrayList();
    //...add some Pair objects to the list
    Collections.shuffle(list);
    

提交回复
热议问题