Java Collection

The Java language supports  a  framework called  collection to store and manipulate groups of objects.Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc). Operation like  insertion ,deletion,sorting, searching can be performed on the  collection(group of object).

Q. WRITE A PROGRAM FOR ADD YOUR FAMILY MEMBER NAME IN AN ARRAYLIST (ArrayList)?

import java.util.*;
public class softaid
{
    public static void main(String[] args) 
	{
		ArrayList family=new ArrayList();
		family.add("RAM");
		family.add("SHYAM");
		family.add("KRISHNA");
		family.add("GOPI");
		family.add("RADHA");
		Iterator itr=family.iterator();
		System.out.print("YOUR FAMILY NAME : ");
		while(itr.hasNext())
		{
			System.out.print("  "+itr.next());
		}
	}
}

Q. : WRITE A PROGRAM FOR STORE USER DEFINED CLASS OBJECT IN ARRAYLIST (ArrayList) ?

import java.util.*;
class Name
{
    String fn,mn,ln; // fn=first name,mn=middle name , ln=last name
	Name(String fn,String mn,String ln)
	{
		this.fn=fn;
		this.mn=mn;
		this.ln=ln;
	}
}
public class softaid 
{
	public static void main(String[] args) 
	{
		ArrayList a=new ArrayList();
		Name n1=new Name("Abhay","Pratap","Singh");
		Name n2=new Name("Samrendra","Pratap","Singh");
		Name n3=new Name("Ved","Prakash","Singh");
		a.add(n1);
		a.add(n2);
		a.add(n3);
		Iterator itr=a.iterator(); 
		while(itr.hasNext())
		{
			Name n=(Name)itr.next();
			System.out.println("FIEST NAME : "+n.fn+"  
			MIDDLE NAME : "+n.mn+"  LAST NAME : "+n.ln);
		}
	}
}

Q. : WRITE A PROGRAM TO REMOVE ALL THE ELEMENTS FROM ArrayList ?

import java.util.*;
public class softaid 
{
    public static void main(String[] args) 
	{
		ArrayList a=new ArrayList();
		
		// ADDING STRING IN ARRAYLIST 
		a.add("THIS");
		a.add("IS");
		a.add("A");
		a.add("JAVA");
		a.add("PROGRAM");
		System.out.println("BEFORE REMOVING ..");
		Iterator itr=a.iterator();
		while(itr.hasNext())
		{
			System.out.print("  " +itr.next());
		}
		
		// REMOVING VALUE FROM ARRAYLIST
		a.removeAll(a);
		
		System.out.println("\n"+" AFTER REMOVING ..");
		Iterator itrr=a.iterator();
		while(itrr.hasNext())
		{
			System.out.print("  " +itr.next());
		}
		System.out.println(" ....empty .....  ");
	}
}

Q. : WRITE A PROGRAM FOR ADD 10 LEADER NAME OF INDIA IN LinkedList ?

import java.util.*;
public class softaid 
{
    public static void main(String[] args) 
	{
		LinkedList ll=new LinkedList();
		ll.add("Dr. Rajendra Prasad ");
		ll.add("Pt Nehru");
		ll.add("Bhagat Singh");
		ll.add("Mahatma Gandhi");
		ll.add("Indra Gandhi");
		ll.add("Sonia Gandhi");
		ll.add("Atal Bihari Vajpayee");
		ll.add("Akhilesh Yadav");
		ll.add("MANMOHAN SINGH");
                ll.add("NARENDRA MODI");
		ll.add("RAHUL GANDHI");
		Iterator itr=ll.iterator();
		
		System.out.println("LIST OF 10 LEADER NAME :  ");
		while(itr.hasNext())
		{
			System.out.println("  "+itr.next());
		}
	}
}

Q. : WRITE A PROGRAM FOR FOR INSERT , DELETE AND UPDATE OF UNIVERSITY NAME IN LIST(LinkedList) ?

import java.util.*;
import java.io.*;
class univ 
{
     LinkedList ll=new LinkedList();
	BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
	 univ()throws IOException
	 {
		 ll.add("OXFORD UNIVERSITY");
         ll.add("JWAHARLAL NEHRU UNIVERSITY ");
         
         
 			
			while(true)
			{
				System.out.println("LIST OF INSERTED UNIVERSITY  ..");
		         Iterator itr=ll.iterator();
		         while(itr.hasNext())
		         {
		        	 System.out.println(itr.next());
		         }
				int choice;
				System.out.println("1 - insert , 2- delete , 3 - update , 4 - exit ");
				System.out.println("ENTER YOUR CHOICE : ");
				choice=Integer.parseInt(bf.readLine());
				if(choice==1)
					{
						String value;
						System.out.println("INSERT YOUR UNIVERSITY  NAME : ");
						value=bf.readLine();
						ll.add(value);
					}
					
				else if(choice==2)
					{
						int index;
						System.out.println("ENTER INDEX NO. THAT U WANT TO DELETE :  ");
						index=Integer.parseInt(bf.readLine());
						ll.remove(index);
					}
					
				else if(choice==3)
					{
						int index;
						String name;
						System.out.println("ENTER INDEX NO. THAT U WANT TO UPDATE  :  ");
						index=Integer.parseInt(bf.readLine());
						System.out.println("ENTER THE  NEW NAME OF UNIVERSITY  FOR UPDATE :  ");
						name=bf.readLine();
						ll.set(index, name);
					}
					
				else if(choice==4)
					{
					System.exit(0);
					}
					
				else
				{
					System.out.println("SORRY ..!! YOU HAVE ENTER WRONG CHOICE   .. ");
				}
						
					
	     		}
 		}				
 }	
public class softaid 
{
	public static void main(String[] args) throws IOException 
	{
		new univ();
	}
}