Created
June 11, 2011 21:59
-
-
Save sachin-handiekar/1021000 to your computer and use it in GitHub Desktop.
Interview Question [Java]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Desired Output | |
--------------- | |
Country - India | |
State - Gujarat | |
City - Ahmedabad | |
City - Vadodara | |
State - MP | |
City - Bhopal | |
City - Indore | |
City - Reva | |
City - Ujjain | |
Country - UK | |
State - Dartford | |
City - Dartford | |
State - Greater London | |
City - Eltham | |
City - London | |
State - Hampshire | |
City - Winchester | |
Country - USA | |
State - Arizona | |
City - Phoenix | |
State - California | |
City - Los Angeles | |
State - Illinois | |
City - Chicago | |
State - New York | |
City - New York | |
State - Texas | |
City - Houston | |
**/ | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.TreeMap; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
public class InterviewQ2 { | |
private TreeMap countryMap = new TreeMap(); | |
private TreeMap stateMap; | |
private ArrayList cityList; | |
/** * Fetch the data */ | |
public void fetchData() { | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
String url = "jdbc:mysql://localhost:3306/db_name"; | |
Connection con = DriverManager.getConnection(url, "username", "password"); | |
Statement stmt = con.createStatement(); | |
ResultSet rs = stmt.executeQuery("SELECT * from countrydata"); | |
while (rs.next()) { | |
String country = rs.getString("country"); | |
String state = rs.getString("state"); | |
String city = rs.getString("city"); | |
if (countryMap.containsKey(country)) { | |
stateMap = (TreeMap) countryMap.get(country); | |
// Check whether the stateMap contains the state or not | |
if (stateMap.containsKey(state)) { | |
cityList = (ArrayList) stateMap.get(state); | |
cityList.add(city); | |
stateMap.put(state, cityList); | |
} else { | |
cityList = new ArrayList(); | |
cityList.add(city); | |
stateMap.put(state, cityList); | |
} | |
countryMap.put(country, stateMap); | |
} else { | |
stateMap = new TreeMap(); | |
cityList = new ArrayList(); | |
cityList.add(city); | |
stateMap.put(state, cityList); | |
countryMap.put(country, stateMap); | |
} | |
} | |
} catch (SQLException ex) { | |
System.out.println(ex.getMessage()); | |
} catch (ClassNotFoundException ex) { | |
System.out.println(ex.getMessage()); | |
} | |
} | |
/** * Print the data */ | |
void printData() { | |
Iterator countryIterator = countryMap.keySet().iterator(); | |
while (countryIterator.hasNext()) { | |
String country = (String) countryIterator.next(); | |
System.out.println("Country - " + country); | |
TreeMap stateMapL = (TreeMap) countryMap.get(country); | |
Iterator stateIterator = stateMapL.keySet().iterator(); | |
while (stateIterator.hasNext()) { | |
String state = (String) stateIterator.next(); | |
System.out.println("\tState - " + state); | |
List<String> stateList = (ArrayList) stateMapL.get(state); | |
if (stateList != null) { | |
Collections.sort(stateList); | |
for (String city : stateList) { | |
System.out.println("\t\tCity - " + city); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment