I am trying to randomly generate 'n' number of items from a HashMap where 'n' is determined by the user.
Here is what I have so far:
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);
}
It prints 1 random color for me (in hex) which I want, however I am unsure of how/which loop to use in order to generate say 3 random hex colors from the map. I declared numColors as 3 just to try and test this out.
Here is what I ended up going with:
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));
}
}