DataStructures : ArrayList, HashMap, Set


ArrayList



- Values could be duplicated

- Use to store multiple "elements" together, represented by single variable name

- The "elements" could be of different Data types such as int, double, Objects
- No need to specify size while declaration, size = Dynamic
- Element can be accessed using array "index"
- Array index starts from zero [0]


--------------------------------------------------------------------------------------
Ex. 1: ArrayList 

Source: TestArrayList.java
import java.util.ArrayList;

public class TestArrayList {
    public static void main(String[] args) {



        ArrayList list = new ArrayList();
   
        list.add("Tokyo");
        list.add("Edogawa ku");
        list.add("Minato ku");
        list.add("Toshima ku");



        Customer cust = new Customer();
        list.add(cust); // object can be added to ArrayList
   
        System.out.println(list.get(3));
  
  }
}



Output
Toshima ku


======================================================

HashMap


- Use to store data in key and value pair 

- Key should be unique and of reference data type
- No need to specify size while declaration, size = Dynamic
---------------------------------------------------------------------------------------
Ex. 2: HashMap 

Source: TestHashMap.java
import java.util.HashMap;

public class 
TestHashMap {
    public static void main(String[] args) {



        HashMap hash = new HashMap();
        //key and value pair (phone/name)
        hash.put("5678", "Yamada");
        hash.put("8934", "Suzuki");
        hash.put("1234", "John");
   
        System.out.println(hash.get("8934"));
    }
}




Output
Suzuki


======================================================

TreeSet



- Use to store unique values, no duplicates


- Use to store multiple "elements" together, represented by single variable name

- The "elements" could be of different Data types such as int, double, Objects
- No need to specify size while declaration, size = Dynamic
- Element can be accessed using array "index"
- Array index starts from zero [0]
----------------------------------------------------------------------------------------
Ex. 3: TreeSet 

Source: TestTreeSet.java
import java.util.TreeSet;

public class TestTreeSet {
    public static void main(String[] args) {

        TreeSet tset = new TreeSet();        
        tset.add(1);
        tset.add(2);
        tset.add(3);
        tset.add(3);
   
        System.out.println(tset.toString());
    
   }
}


Output
[1,2,3]

Application : Calculator (For 3 Numbers)

Requirements:
Create Calculator Application to have following functionality,
- Add 3 numbers
- Subtract 3 numbers
- Divide 3 numbers 
- Multiply 3 numbers


- Take command line input for 3 numbers
- User can enter option based on the menu as shown below,

Menu 
(1=Add, 2=Sub, 3=Div, 4=Mul )


Design:

Implementation:
[CalculatorApp.java]
import java.util.Scanner;

public class CalculatorApp {

    public static void main(String[] args) {



        //read command line arguments from the command line while running this application
        double n1 = Double.parseDouble(args[0]);  //first number
        double n2 = Double.parseDouble(args[1]);  //second number
        double n3 = Double.parseDouble(args[2]);  //third number
        

        //use to get user input for operation
        Scanner scan = new Scanner(System.in);

    
        System.out.println("Menu ");
        System.out.println("(1=Add, 2=Sub, 3=Div, 4=Mul )");

        int option = scan.nextInt();
   
        CalculatorApp appC = new CalculatorApp();

        switch (option) {

        case 1 :
            appC.add(n1, n2, n3);
            break;

        case 2 :
            appC.sub(n1, n2, n3);
            break;

        case 3 :
            appC.div(n1, n2, n3);
            break;
        case 4 :
            appC.mul(n1, n2, n3);
            break;

        default :
            System.out.println("Invalid option.");
        }
   
    } //end of main() method

    //Method to add three numbers and display results
    public void add(double n1, double n2, double n3) {
        System.out.println("Adding...");
        double result = n1 + n2 + n3;
        System.out.println(result);
    }

    
 //Method to subtract three numbers and display results    

    public void sub(double n1, double n2, double n3) {
        System.out.println("Subtracting...");
        double result = n1 - n2 - n3;
        System.out.println(result);
    }

    //Method to divide three numbers and display results  
    public void div(double n1, double n2, double n3) {
        System.out.println("Dividing...");
        double result = n1 / n2 / n3;
        System.out.println(result);
    }

   //Method to multiply three numbers and display results  
    public void mul(double n1, double n2, double n3) {
        System.out.println("Multiplying...");
        double result = n1 * n2 * n3;
        System.out.println(result);
    }

}



Testing:
=============How to Compile using java command line without Eclipse
javac CalculatorApp.java
The above command will compile CalculatorApp.java to CalculatorApp.class
=============How to Run using Command prompt (such as dos/unix/linux) 
java CalculatorApp 1 2 3
The above command will run CalculatorApp application with 3 arguments (Number1, Number2 and Number3)
=============Output: For Add option [other 3 options (Sub, Div, Mul) ]
Menu 
(1=Add, 2=Sub, 3=Div, 4=Mul )


1
Adding...
6.0