Question: PLEASE DO A STEP BY STEP DETAILED CODE TRACE HOW THIS GETS EXECUTED // Java program to print itinerary in order import java.util.HashMap; import java.util.Map;

PLEASE DO A STEP BY STEP DETAILED CODE TRACE HOW THIS GETS EXECUTED

// Java program to print itinerary in order

import java.util.HashMap;

import java.util.Map;

public class printItinerary

{

// Driver function

public static void main(String[] args)

{

Map dataSet = new HashMap();

dataSet.put("Chennai", "Banglore");

dataSet.put("Bombay", "Delhi");

dataSet.put("Goa", "Chennai");

dataSet.put("Delhi", "Goa");

printResult(dataSet);

}

// This function populates 'result' for given input 'dataset'

private static void printResult(Map dataSet)

{

// To store reverse of given map

Map reverseMap = new HashMap();

// To fill reverse map, iterate through the given map

for (Map.Entry entry: dataSet.entrySet())

reverseMap.put(entry.getValue(), entry.getKey());

// Find the starting point of itinerary

String start = null;

for (Map.Entry entry: dataSet.entrySet())

{

if (!reverseMap.containsKey(entry.getKey()))

{

start = entry.getKey();

break;

}

}

// If we could not find a starting point, then something wrong

// with input

if (start == null)

{

System.out.println("Invalid Input");

return;

}

// Once we have starting point, we simple need to go next, next

// of next using given hash map

String to = dataSet.get(start);

while (to != null)

{

System.out.print(start + "->" + to + ", ");

start = to;

to = dataSet.get(to);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!