ARTICLE AD BOX
Playing around with HashMap and learning how to organize it, but got stuck trying to print out a random value in my map. Used youtube Hashmap tutorial and this article to base the code. Selecting random key and value sets from a Map in Java
package Random; import java.util.HashMap; import java.util.List; import java.util.Random; import java.util.ArrayList; public class MoviesList { public static void main(String[] args){ HashMap<String,Integer> moviesToWatch = new HashMap<>(); moviesToWatch.put("American Psycho", 1); moviesToWatch.put("Fight Club", 2); moviesToWatch.put("Akira", 3); moviesToWatch.put("SEVEN", 4); System.out.println(moviesToWatch); System.out.println(moviesToWatch.get("Akira")); System.out.println(moviesToWatch.containsKey("Akira")); System.out.println(moviesToWatch.containsValue(8)); Random random = new Random(); List<String> keys = new ArrayList<String>(moviesToWatch.keySet()); String randomKey = keys.get( random.nextInt(keys.size()) ); String value = moviesToWatch.get(randomKey); } }