LIST OF JAVA COLLECTION FRAMEWORK PROGRAMS

Collection framework in java is a collection of classes and interfaces.

Program 1:

Write a program to store the element in the ArrayList.

import java.util.*;
public class ArrayListProgram
{
public static void main(String[] arg)
{
ArrayList<String> obj=new ArrayList<String>();//Creating the ArrayList
obj.add("suman");//adding the element into the ArrayList
obj.add("kumnar");
obj.add("amit");
obj.add("ram");
obj.add("ram");
System.out.print(obj);//printing the element
}
}

Output:

[suman,kumar,amit,ram,ram]

Note:

We know that list contains the duplicate element means same element.
so ArrayList also contains the duplicate elements.

Program 2:

Write a program to store five student name and their roll number.

Here we will store student name and roll number in the ArrayList.S

public class Student
{
private String name;
private int rollNo;
public Student(String name,int rollNo)
{
this.name=name;
this.rollNo=rollNo;
}
}
import java.util.*;
public class StudentDetailsExample
{
public static void main(String[] arg)
{
//calling the student class and put the data
Student obj1=new Student("suman",123);
Student obj2=new Student("kumar",124);
Student obj3=new Student("sunil",125);
Student obj4=new Student("amit",126);
Student obj5=new Student("ashish",127);
//creating the arraylist
ArrayList<Student> student=new ArrayList<Student>();
student.add(obj1);
student.add(obj2);
student.add(obj3);
student.add(obj4);
student.add(obj5);
Iterator iterator=student.iterator();
while(iterator.hasNext()
{
Student str=(Student)iterator.next();
System.out.println(str.name +" "+str.rollNo);
}
}
}

Output:

suman 123
kumar 124
sunil 125
amit  126
ashish 127

Program 3:

Write a program of set in java.

Set is an interface which does not allow the duplicate element.

import java.util.*;
Public class SetProgramExample
{
Public static void main(String[] arg)
{
Set<String> obj=new HashSet<String>();
obj.add(“shivam”);
obj.add(“placement”);
obj.add(“ashish”);
obj.add(“tanuj”);
obj.add(“shivam”);
System.out.println(“Output of the above program”);
System.out.print(obj);

//TreeSet is used to sort the element in ascending order

System.out.println(“Element in the sorted order:”);
Set<String> sortedString=new TreeSet<String>(obj);
System.out.println(sortedString);
}
}

Output:

Output of the above program

[placement,ashish,shivam,tanuj]

Element in the sorted order

[ashish,placement,tanuj,shivam]

Program 4:

Write a program to remove the element from the specified position in ArrayList.

import java.util.*;
public class RemoveElement
{
public static void main(String[] arg)
{

ArrayList<String> obj=new ArrayList<String>();
obj.add("suman");
obj.add("kumar");
obj.add("raj");
obj.add("amit");
System.out.println("Element list before the Remove:")
System.out.println(obj);
obj.remove(0);
obj.romove(1);
System.out.println("Element list after the Remove:");
System.out.println(obj);
}
}

Output:

Element list before the Remove
suman
kumar
raj
amit

Element list after the Remove:
raj
amit

Program 5:

Write a program to sort the ArrayList element.

We can sort the ArrayList element using the Collections.sort( ) class.

public class SortArrayListElement
{
public static void main(String[] rag)
{
ArrayList<Interger> obj=new ArrayList<Integer>();
obj.add(9);
obj.add(8);
obj.add(4);
obj.add(7);
System.out.println(" ArrayList before the sorting");
System.out.println(obj);

Collections.sort(obj);//Collections.sort() is used to sort the ArrayList element

System.out.println("ArrayList after the sorting");
System.out.println(obj);
}
}

Output:

ArrayList before the sorting
9
8
4
7
ArrayList after the sorting
4
7
8
9

Program 6:

Write a program to add the element into the HashMap.

HashMap is used for the store the element in key-value pairs.

import java.util.*;
public class HashMapExample
{
public static void main(String[] arg)
{
HashMap<Integer,String> obj=new HashMap<Integer,Steing>();
obj.put(1,"suman");
obj.put(2,"kumar");
obj.put(3,"amit");
obj.pu(4,"raj");

//Traversing the above element
//Here we will use the 'entrySet()' method which provides the two method getKey() & getValue() 
for(Map.entey mapelement:obj.entrySet()) 
{
System.out.println(mapelement:obj.getKey()+ " "+mapelement:obj.getvalue());
}
}
}

Output:

1 suman
2 kumar
3 raj
4 amit

Program 7:

Write a program to Add and Remove the element from the Hashtable.

import java.util.*;
public class HashtableAddOrRemove
{
public static void main(String[] arg)
{
Hashtable<Integer,String> obj=new Hashtable<Integer,String>();
obj.put(1,"suman");
obj.put(2,"kumar");
obj.put(3,"raj");
obj.add(4,"amit");
System.out.println("Element list:");
for(Map.entry obj1:obj.entrySet())
{
System.out.println(obj1.getKey+" "+obj1.getValue());
}
obj.remove(1);
System.out.println("Element after the remove:");
for
System.out.println(obj1);
}
}
}

Output:

Element list:
1 suman
2 kumar
3 raj
4 amit

Element after the remove:
2 kumar
3 raj
4 amit

Program 8:

Write a program to traverse the ArrayList element in both the direction forward and backward

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *