从地图中随机选择n个项目

时间:2014-08-06 18:43:26

标签: java list random arraylist hashmap

我试图从HashMap中随机生成'n'个项目,其中'n'由用户决定。

这是我到目前为止所做的:

public static void main(String []args){
    int numColors = 3;
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("White","FFFFFF");
    map.put("Blank","000000");
    map.put("Red","ED0A15");
    map.put("Green","06F76C");
    map.put("Blue","0689FF");
    map.put("Sky Blue","00C2FC");
    map.put("Light Blue","08F0FC");
    map.put("Silver","C0BFC5");
    map.put("Mint","ABD3CA");
    map.put("Off White","FFEFF0");
    map.put("Purple","736FFA");
    map.put("Lavendar","DEBEEF");
    map.put("Hot Pink","F5159A");
    map.put("Pink","DB39CC");
    map.put("Light Pink","F5C2E3");
    map.put("Blush","C95FA7");
    map.put("Orange","D4361B");
    map.put("Yellow","DEF231");
    map.put("Warm White","F3E4C3");
    map.put("Turquoise","01DCA4");

    List<String> valuesList = new ArrayList<String>(map.values());
    int randomIndex = new Random().nextInt(valuesList.size());
    String randomValue = valuesList.get(randomIndex);

    System.out.printf(randomValue);
}

它为我打印了1个随机颜色(十六进制),但是我不确定如何/使用哪个循环来从地图生成3个随机的十六进制颜色。我将numColors声明为3只是为了尝试测试它。

以下是我最终的目的:

public static void main(String []args){
    int numColors = 3;
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("White","FFFFFF");
    map.put("Blank","000000");
    map.put("Red","ED0A15");
    map.put("Green","06F76C");
    map.put("Blue","0689FF");
    map.put("Sky Blue","00C2FC");
    map.put("Light Blue","08F0FC");
    map.put("Silver","C0BFC5");
    map.put("Mint","ABD3CA");
    map.put("Off White","FFEFF0");
    map.put("Purple","736FFA");
    map.put("Lavendar","DEBEEF");
    map.put("Hot Pink","F5159A");
    map.put("Pink","DB39CC");
    map.put("Light Pink","F5C2E3");
    map.put("Blush","C95FA7");
    map.put("Orange","D4361B");
    map.put("Yellow","DEF231");
    map.put("Warm White","F3E4C3");
    map.put("Turquoise","01DCA4");

    List<String> keys = new ArrayList<String>(map.keySet());
    Random rand = new Random();
    for (int i = 0; i < numColors; i++) {
        String key = keys.get(rand.nextInt(keys.size()));
        System.out.println(map.get(key));
        }

 }

4 个答案:

答案 0 :(得分:2)

一个简单的解决方案是使用Collections.shuffle(map)对整个地图进行随机播放。然后迭代它并选择第一个 n 元素。

当然,如果地图很大并且你只需要几个元素,这是没有意义的。

编辑:

当然,使用此解决方案,您将无法获得任何重复的条目

答案 1 :(得分:1)

如果我理解你的问题,你可以用

来做
List<String> keys = new ArrayList<String>(map.keySet());
Random rand = new Random();
for (int i = 0; i < numColors; i++) {
    String key = keys.get(rand.nextInt(keys.size()));
    System.out.println(map.get(key));
}

答案 2 :(得分:0)

评论中提到了更改

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        int numColors = 3;
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("White", "FFFFFF");
        map.put("Blank", "000000");
        map.put("Red", "ED0A15");
        map.put("Green", "06F76C");
        map.put("Blue", "0689FF");
        map.put("Sky Blue", "00C2FC");
        map.put("Light Blue", "08F0FC");
        map.put("Silver", "C0BFC5");
        map.put("Mint", "ABD3CA");
        map.put("Off White", "FFEFF0");
        map.put("Purple", "736FFA");
        map.put("Lavendar", "DEBEEF");
        map.put("Hot Pink", "F5159A");
        map.put("Pink", "DB39CC");
        map.put("Light Pink", "F5C2E3");
        map.put("Blush", "C95FA7");
        map.put("Orange", "D4361B");
        map.put("Yellow", "DEF231");
        map.put("Warm White", "F3E4C3");
        map.put("Turquoise", "01DCA4");

        // scanner for accepting values
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number");
        int N = scan.nextInt();

        // random object for generating random values
        Random rand = new Random();

        // converting map values to list
        List<String> valuesList = new ArrayList<String>(map.values());

        for (int i = 1; i <= N; i++) {
            // choose random value
            int randomIndex = rand.nextInt(valuesList.size());
            // get value
            String randomValue = valuesList.get(randomIndex);
            // printing
            System.out.println("Random value " + i + " : " + randomValue);
        }
    }
}

为防止重复,您可以执行以下操作:

// random object for generating random values
Random rand = new Random();

// converting map values to list
List<String> valuesList = new ArrayList<String>(map.values());

Set<String> set = new HashSet<String>();

while (set.size() != N) {
    int randomIndex = rand.nextInt(valuesList.size());
    String randomValue = valuesList.get(randomIndex);
    set.add(randomValue);
}

System.out.println(set);

答案 3 :(得分:0)

正如Malt建议的那样,为了防止重复并保持代码清洁:

List<String> list = new ArrayList(map.values() );
Collections.shuffle(list);