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

后端 未结 5 2067
抹茶落季
抹茶落季 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<Pair> list = new ArrayList<Pair>();
    //...add some Pair objects to the list
    Collections.shuffle(list);
    
    0 讨论(0)
  • 2021-02-06 04:14

    You could keep a separate List of the keyvalues, shuffle that and use it to access the HashMap.

    List<Integer> keys = new ArrayList<Integer>(map.keySet());
    Collections.shuffle(keys);
    for(Integer i : keys)
        map.get(i);     // Gets the values in the shuffled order
    
    0 讨论(0)
  • 2021-02-06 04:20

    You can use enum to achieve this, example

    package device.packet.data;
    
    public enum TLVList {
        
        SpeedingAlarm(0x1001, 3, Short.class),
        LowVoltageAlarm(0x1002, 3, Short.class),
        OBDCommunicationError(0x1015, 3, Short.class),
        IgnitionOn(0x1016, 3, Short.class),
        IgnitionOff(0x1017, 3, Short.class),
        MILAlarm(0x1018, 3, Short.class),
        UnlockAlarm(0x1019, 3, Short.class),
        NoCardPresented(0x101A, 3, Short.class),
        HiddenCommand(0x101B, 3, Short.class),
        Vibration(0x101C, 3, Short.class),
        DoorOpened(0x101D, 3, Short.class);
        
        private int tagFlagHex;
        private int length;
        private Class aClass;
    
        public int gettagFlagHex() {
            return tagFlagHex;
        }
    
        public int getLength() {
            return length;
        }
    
        public Class getClassType() {
            return aClass;
        }
    
        TLVList(int tagFlagHex, int length, Class aClass) {
    
            this.tagFlagHex = tagFlagHex;
            this.length = length;
            this.aClass = aClass;
        }
    
        public static TLVList of(Integer tagFlagHex) {
            for (TLVList tagFlag : TLVList.values()) {
                if(tagFlag.tagFlagHex == tagFlagHex) return tagFlag;
            }
            return null;
        }
    }
    

    I hope it helps.

    0 讨论(0)
  • 2021-02-06 04:23

    You can keep the Map. The Map is designed to be looked up by key so I suggest you have a list of shuffled keys.

    public Map<Integer, String> getQuestionOptionsMap() {
        Map<Integer, String> map = new HashMap<>();
        String[] answers = {null, answer1, answer2, answer3, answer4};
        for (int i = 1; i < answers.length; i++)
            if (answers[i] != null)
                map.put(i, answers[i]);
        List<Integer> order = new ArrayList<>(map.keySet());
        Collections.shuffle(order);
        Map<Integer, String> shuffled = new LinkedHashMap<>();
        for (Integer key : order)
            shuffled.put(key, map.get(key));
        return shuffled;
    }
    
    0 讨论(0)
  • 2021-02-06 04:30
    Hashtable<Integer, String>
    

    As an alternative to less convenient and effective List<SomePairClass<Integer, String>>

    0 讨论(0)
提交回复
热议问题