A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Create an ArrayList that should contain element in key-value pair(map list) at each index. Clean the list.

Programming steps:


1. Create an ArrayList:


ArrayList<HashMap>  mylist = new ArrayList<>();

2. element in key-value pair(map list) :


        HashMap  hm = new HashMap();
        hm.put(101, "Aman");
     
        HashMap  hm1 = new HashMap();
        //hm1.put(102, "Chetanya");
       
        HashMap  hm2 = new HashMap();
        hm2.put(103, "Ravish");


3. element in key-value pair(map list) at each index.

        mylist.add(hm);       

        mylist.add(1,hm2);
       
        mylist.add(2,hm1);
       

4. Clean the list if not empty:

        if(mylist.isEmpty())

        {
            System.out.println("list index 2 is empty..");
        }
        else
        {
            mylist.clear();
            System.out.println("Now list is empty");
        }


Complete Program code;
import java.util.HashMap;
import java.util.ArrayList;
public class INT
{
    public static void main(String args[])
    {
        HashMap  hm = new HashMap();
        hm.put(101, "Aman");
     
        HashMap  hm1 = new HashMap();
        //hm1.put(102, "Chetanya");
       
        HashMap  hm2 = new HashMap();
        hm2.put(103, "Ravish");
       
        ArrayList<HashMap>  mylist = new ArrayList<>();
        mylist.add(hm);
       
        mylist.add(1,hm2);
       
        mylist.add(2,hm1);
       // mylist.add(123);
       
        System.out.println("list is: "+mylist.get(2));
        if(hm1.size()==0)
        {
            System.out.println("map empty");
        }
       
        if(mylist.get(2).isEmpty())
        {
            System.out.println("list index 2 is empty..");
        }
       
        System.out.println("list is "+mylist);
        if(mylist.isEmpty())
        {
            System.out.println("list index 2 is empty..");
        }
        else
        {
            mylist.clear();
            System.out.println("Now list is empty");
        }
    }
   
}

LEARN TUTORIALS

.

.