Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions

Question

Define self-esteem and discuss its impact on your life.

Answered: 1 week ago