In: Computer Science
package datastructure;
public class UseMap {
   public static void main(String[] args) {
      /*
       * Demonstrate how to use Map that includes storing and retrieving elements.
       * Add List<String> into a Map. Like, Map<String, List<string>> list = new HashMap<String, List<String>>();
       * Use For Each loop and while loop with Iterator to retrieve data.
       *
       * Use any databases[MongoDB, Oracle, MySql] to store data and retrieve data.
       */
   }
}




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datastructure;
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.HashMap;
import java.util.List;
import java.util.Map;
/**
 *
 * @author sysadmin
 */
public class Datastructure {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Map<String, List<String>> list = new HashMap<String, List<String>>();
        try {
           Class.forName("oracle.jdbc.driver.OracleDriver");// load driver class
           //Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");        //connection string for apahce derby database    
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "orcl", "orcl");//I used oracle database and this is connection string for oracle database
            Statement stmt_cust = con.createStatement(); //create statement object for customer table
            Statement stmt_orders = con.createStatement(); //Create statement object for orders table
            ResultSet rs = stmt_cust.executeQuery("select cid,name from customers"); //Execute query and create resultset object for customers table
            while (rs.next()) {
                int cust_id = rs.getInt(1);
                String cust_name = rs.getString(2);
                ResultSet rs_order = stmt_orders.executeQuery("select item_name from orders where orders.cid=" + cust_id); //Execute query fro orders table
                List<String> item_name = new ArrayList<>();
                while (rs_order.next()) {
                    item_name.add(rs_order.getString(1));
                }
                if (!item_name.isEmpty()) {
                    list.put(cust_name, item_name); //Store customer_name and corresponding item ordered ..in the map.
                }
                rs_order.close();
            }
            rs.close();
            stmt_cust.close();
            stmt_orders.close();
            for (Map.Entry<String, List<String>> entry : list.entrySet()) { ///iterate through the map
                String key = entry.getKey();
                for (String value : entry.getValue()) {
                    System.out.println("Customer_Name:" + key + "\t Item Name:" + value);
                }
            }
        } catch (Exception exception) {
            System.out.println("There is a error in:" + exception.getMessage()); // execption handling
        }
    }
}