Maps (Dictionaries)

  • Associate key with value
  • Key & value can be any data type
  • Easily searchable by key
  • Store and retrieve data quickly

The program will store a map that associates lines of input with random integers. When a line of input is read, the program will check to see if that line has already been used as a key in the map. If so, it will return the value associated with that key.

If the map does not contain an entry for that key, the program should generate a new random integer and store it in the map, using the input line as the key.


import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){
} }



import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){ if(map == null){ map = new HashMap<String, Integer>(); } } }


import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){ if(map == null){ map = new HashMap<String, Integer>(); } } }

import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){ if(map == null){ map = new HashMap<String, Integer>(); } } }


import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){ if(map == null){ map = new HashMap<String, Integer>(); } } }


import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){ if(map == null){ map = new HashMap<String, Integer>(); } } }


import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){ if(map == null){ map = new HashMap<String, Integer>(); } if(random == null){ random = new Random(); } } }


import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){ if(map == null){ map = new HashMap<String, Integer>(); } if(random == null){ random = new Random(); } if(map.containsKey(inp)){
} } }



import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){ if(map == null){ map = new HashMap<String, Integer>(); } if(random == null){ random = new Random(); } if(map.containsKey(inp)){ return map.get(inp); } } }

import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){ if(map == null){ map = new HashMap<String, Integer>(); } if(random == null){ random = new Random(); } if(map.containsKey(inp)){ return map.get(inp); }else{ int newNumber = random.nextInt();

} } }


import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){ if(map == null){ map = new HashMap<String, Integer>(); } if(random == null){ random = new Random(); } if(map.containsKey(inp)){ return map.get(inp); }else{ int newNumber = random.nextInt(); map.put(inp, newNumber);
} } }

import java.util.Map;
import java.util.HashMap;
import java.util.Random;
public class MapExample{
public static Map<String, Integer> map; public static Random random;
public static int getOutput(String inp){ if(map == null){ map = new HashMap<String, Integer>(); } if(random == null){ random = new Random(); } if(map.containsKey(inp)){ return map.get(inp); }else{ int newNumber = random.nextInt(); map.put(inp, newNumber); return newNumber; } } }