In: Computer Science
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.TreeSet; /** * MapSetLab - test the HashMap and TreeSet classes. * * @author Michael Norton * @version 18 September 2018 */ public class MapSetLab { /** * Main method. * * @param args The command line arguments * @throws IOException The IOException */ public static void main( String[] args ) throws IOException { // declarations & some instantiations BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ); HashMap< String, TreeSet< String > > networkMap = new HashMap< String, TreeSet< String> >(); // reusable references String network; String tvShow; TreeSet< String > set; // get 10 network & tv show pairs for ( int i = 0; i < 10; i++ ) { // get values from user System.out.print( "Enter TV network: " ); network = reader.readLine(); System.out.print( "Enter TV show on " + network + ": " ); tvShow = reader.readLine(); // if key not found, create new entry if ( !networkMap.containsKey( network ) ) { networkMap.put( network, new TreeSet<String>() ); } // get the TreeSet from the HashMap and add the show to the network set = networkMap.get( network ); set.add( tvShow ); // print out the mapping System.out.println( networkMap ); System.out.println(); } } }
Use an appropriate loop to ask the user to enter the name of a TV show and then print on a separate line the name of the network that maps to that show. Repeat until the user enters an empty string.
HINT: Iterate over each key (network) in the HashMap, and see if its value (set of shows) contains the desired show. If so, print out the network. If none of the networks have that show, report "UNKNOWN". You don't have to worry about uppercase/lowercase, just match the TV show name exactly.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.TreeSet;
/**
* MapSetLab - test the HashMap and TreeSet classes.
*
* @author Michael Norton
* @version 18 September 2018
*/
public class MapSetLab {
/**
* Main method.
*
* @param args The command line arguments
* @throws IOException The IOException
*/
public static void main(String[] args) throws IOException {
// declarations & some instantiations
BufferedReader reader = new BufferedReader( new InputStreamReader(
System.in ) );
HashMap< String, TreeSet< String > > networkMap =
new HashMap< String, TreeSet< String> >();
// reusable references
String network;
String tvShow;
TreeSet< String > set;
// get 10 network & tv show pairs
for ( int i = 0; i < 10; i++ ) {
// get values from user
System.out.print( "Enter TV network: " );
network = reader.readLine();
System.out.print( "Enter TV show on " + network + ": " );
tvShow = reader.readLine();
// if key not found, create new entry
if ( !networkMap.containsKey( network ) ) {
networkMap.put( network, new TreeSet<String>() );
}
// get the TreeSet from the HashMap and add the show to the network
set = networkMap.get( network );
set.add( tvShow );
// print out the mapping
System.out.println( networkMap );
System.out.println();
}
boolean found ;
// loop that continues till the user exits
while(true)
{
found = false;
// input of tv show name
System.out.print("Enter the TV show name : ");
tvShow = reader.readLine();
// check if user entered an empty string, then exit from loop
if(tvShow.length() == 0)
break;
// loop over the map
for( String tvNetwork: networkMap.keySet())
{
// check if this network contains the tv show, then print the network and exit from loop
if(networkMap.get(tvNetwork).contains(tvShow))
{
System.out.println(tvNetwork);
found=true;
break;
}
}
// check if tv show network was found, if not print "UNKNOWN"
if(!found )
System.out.println("UNKNOWN");
}
reader.close();
}
}
//end of MapSetLab.java
Output: