Java 要混洗的键值(整数、字符串)列表的最佳结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18782110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:01:09  来源:igfitidea点击:

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

javahashmaphashtableshufflelinkedhashmap

提问by MDT

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

我需要在 Java 中实现一个结构,它是一个键值列表(类型为 Integer-String),我想对其进行洗牌。

Basically, I would like to do something like that.

基本上,我想做这样的事情。

    public LinkedHashMap<Integer, String> getQuestionOptionsMap(){

    LinkedHashMap<Integer, String>  shuffle = new LinkedHashMap<Integer, String> ();

    if (answer1 != null)
        shuffle.put(new Integer(1), answer1);
    if (answer2 != null)
        shuffle.put(new Integer(2), answer2);
    if (answer3 != null)
        shuffle.put(new Integer(3), answer3);
    if (answer4 != null) 
        shuffle.put(new Integer(4), answer4);

    Collections.shuffle(shuffle);
    return shuffle;
}

However, HashMap cannot be shuffled.

但是,HashMap 不能混洗。

I could randomly get a key from the hashmap, and then return the linked element, but I'm sure this is not the best solution for my problem.

我可以从哈希图中随机获取一个键,然后返回链接的元素,但我确信这不是解决我的问题的最佳解决方案。

Is there any better way?

有没有更好的办法?

Thanks in advance.

提前致谢。

采纳答案by Konstantin Yovkov

Create a Pairclass, that holds both the Integerand the Stringand then add multiple Pairobjects to a List, which will be shuffled.

创建一个Pair包含 theInteger和 the的类,String然后将多个Pair对象添加到一个列表中,该列表将被打乱。

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);

回答by Kayaman

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

您可以保留一个单独List的键值,对其进行混洗并使用它来访问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

回答by Peter Lawrey

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.

您可以保留地图。Map 设计为按键查找,所以我建议你有一个洗牌的键列表。

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;
}

回答by Oleg Mikhailov

Hashtable<Integer, String>

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

作为不太方便和有效的替代品List<SomePairClass<Integer, String>>