• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Collection Framework


Collection Object
If any object is collection of hetrogeneous objects or elements is known as collection object.
Collection Class
Every collection object can be derived from a special class called collection class.
Collection Framework
collection of collection classes is known as collection framework.

In java language all collection classes are existing as predefined package in java.util package.
The main aim of collection framework is to manage the data very efficiently in the memory( managing data represents searching the data, sorting data etc..)
Note:
In java language data structure (managing the data in the memory) can be achieved by collection framework. In java language collection framework mainly depends on following interfaces.
1.List Interface
2.Set Interface
3.Map Interface

List Interface:
it is a predefined interface in java.util package. It accepts duplicate data in the memory. which was implemented following collection classes.
1.Array List
2.Vector
3.Stack
4.Linked List
etc...
objects of above collection calsses accepts hetrogeneous data.

Set Interface:
it is a predefined interface in java.util package. It accepts unique data in the memory. which was implemented following collection classes.
1.HashSet
2.LinkedHashSet

Map Interface:
it is a predefined interface in java.util package. It accepts elements in the form of key value pair(key allows unique elements where as value allows duplicate elements). which was implemented following collection classes.
1.HashMap
2.HashTable

All the collection classes are existing as templete of generic classes.In java.util package to allocate sufficient memory space we must create an object for any collection class with the following syntax
collectionclass<wrapperclass> objref=new collectionclass<wrapperclass>();

In the above syntax based on wrapper class is allocates sufficient memory for that related element.
Example:
Stack<Integer> objref=new Stack<Integer>();
In the above example stack can be accept only integer elements.
Stack<String> objref=new Stack<String>();
In the above example stack can be accept only String elements.
Stack objref=new Stack();
In the above example stack can be accept different types of elements.

Map Interface Syntax:
collectionclass<wrapperclass1,wrapperclass2> objref=new collectionclass1<wrapperclass1,wrapperclass2>();
in the above syntax
wrapperclass1 is a key
wrapperclass2 is a value
Example:
HashMap<Integer,String> objref=new HashMap<Integer,String>();


View Image

Key Points

Image






List Interface:


ArrayList :
ArrayList is a growable array and which allows dynamic opreations like insertion,deletion,searching,sorting etc..
ArrayList always accepts duplicate elements, memory can be allocated by creating an object as shown below.
ArrayList<Integer> al=new ArrayList<Integer>();
Initially memory is allocated for elements later that can be growable to any number of elements
ArrayList class contains various methods to perform various dynamic operations on the elements.


ArrayList Methods
1.ArrayList Add
2.ArrayList AddIndex
3.ArrayList AddAll
4.ArrayList Clear
5.Array ListClone
7.ArrayList Contains
8.ArrayList Get
9.ArrayList IndexOf
10.ArrayList IsEmpty
11.ArrayList Remove
12.ArrayList Set
13.ArrayList Size
14.ArrayList ToArray



ArrayList Add Method:
Which can be used to either add or insert the element.
Syntax:
al.add(element);
Which can be used to add the an element at the endof arraylist.


import java.util.*;
class ArrayListAdd
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
}
}
class Main
{
public static void main(String aargs[])
{
ArrayListAdd d=new ArrayListAdd();
d.list();
}
}


Output:

ArrayList AddIndex Method:
Which can be used to insert a new element at the given index value.
Syntax:
al.add(index,element);


import java.util.*;
class ArrayListAddIndex
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
al.add(2,2000);//it will insert 2 index element
System.out.println("The List Items Are "+al);
}
}
class Main
{
public static void main(String aargs[])
{
ArrayListAddIndex d=new ArrayListAddIndex();
d.list();
}
}


Output:

ArrayList AddAll Method:
Which can be used to add all elements of one arraylist to another arraylist.
Syntax: al2.addAll(al1);
Elements of al1 objects will be added to al2 object.


import java.util.*;
class ArrayListAddAll
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al1.add(20);
al1.add(30);
al1.add(40);
al1.add(10);
al1.add(70);
System.out.println("The List Items Are "+al1);
ArrayList<Integer> al2=new ArrayList<Integer>();
al2.addAll(al1);
System.out.println("The List Items Are "+al2);
}
}
class Main
{
public static void main(String aargs[])
{
ArrayListAddAll d=new ArrayListAddAll();
d.list();
}
}


Output:

ArrayList Clear Method :
Which can be used to remove all the elements from an arraylist. Syntax:
al.clear();


import java.util.*;
class ArrayListClear
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al1.add(20);
al1.add(30);
al1.add(40);
al1.add(10);
al1.add(70);
System.out.println("The List Items Are "+al1);
al1.clear();
System.out.println("The List Items Are "+al1);
}
}
class Main
{
public static void main(String aargs[])
{
ArrayListClear d=new ArrayListClear();
d.list();
}
}


Output:

Array List Clone Method
Which can be used to get the get cloning object or duplicate object from the existing array list object.
Syntax: ArrayList<Integer> al2;
al2=(ArrayList)al.clone();
Duplicate object is created for 'al1' for whose reference is assigned to al2.

Difference between clone and addAll methods? Close method will create duplicate memory from the existing memory. Where as addAll elements one existing object to another existing object.


import java.util.*;
class ArrayListClone
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
ArrayList<Integer> al2;
al2=(ArrayList)al.clone();
System.out.println("After Cloning The List Items Are "+al2);
}
}
class Main
{
public static void main(String aargs[])
{
ArrayListClone d=new ArrayListClone();
d.list();
}
}


Output:

ArrayList Contains Method :
It is a boolean method,it return true if the given element existing in the list otherwise returns false.
al.contains(element);


import java.util.*;
class ArrayListContains
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.contains(20));
}
}
class Main
{
public static void main(String args[])
{
ArrayListContains d=new ArrayListContains();
d.list();
}
}


Output:

ArrayList Get Method
It is used to find element based on index value.
al.get(indexvalue);


import java.util.*;
class ArrayListGet
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.get(1));
}
}
class Main
{
public static void main(String aargs[])
{
ArrayListGet d=new ArrayListGet();
d.list();
}
}


Output:

ArrayList IndexOf Method
Which can be used to find the index value based on the element or object.
al.indexOf(element);


import java.util.*;
class ArrayListIndexOf
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.indexOf(10));
}
}
class Main
{
public static void main(String aargs[])
{
ArrayListIndexOf d=new ArrayListIndexOf();
d.list();
}
}


Output:

ArrayList IsEmpty Method
Which can be used to it return true the list is empty otherwise it returns false.
al.isEmpty();


import java.util.*;
class ArrayListIsEmpty
{
void list()
{
AArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.isEmpty());
}
}
class Main
{
public static void main(String args[])
{
ArrayListIsEmpty d=new ArrayListIsEmpty();
d.list();
}
}


Output:

ArrayList Remove Method
Which can be used to remove a specific element based on index value.
al.remove(indexvalue);
It can be removed not only based on index value but also it is possible throw element or value.


import java.util.*;
class ArrayListRemove
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
al.remove(2);
System.out.println("After Remove The List Items Are "+al);
}
}
class Main
{
public static void main(String args[])
{
ArrayListRemove d=new ArrayListRemove();
d.list();
}
}


Output:

ArrayList Set Method
Which can be used to replace a new element with old element based on the index value.
al.set(indexvalue,element);


import java.util.*;
class ArrayListSet
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
al.set(1,1000);
System.out.println("After Set The List Items Are "+al);
}
}
class Main
{
public static void main(String args[])
{
ArrayListSet d=new ArrayListSet();
d.list();
}
}


Output:

ArrayList Size Method:
Which can be used to find the size of array list(number of elements of an arraylist)
al.size();


import java.util.*;
class ArrayListSize
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.size());
}
}
class Main
{
public static void main(String args[])
{
ArrayListSize d=new ArrayListSize();
d.list();
}
}


Output:

ArrayList ToArray Method:
It returns any array containing all the elements of arraylist so that can be use the element of array but no manifulation can be done.
al.toArray();


mport java.util.*;
class ArrayListToArray
{
void list()
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.toArray());
}
}
class Main
{
public static void main(String aargs[])
{
ArrayListToArray d=new ArrayListToArray();
d.list();
}
}
Note: For all the list intrface implemented classes 50% of current location on increased once default memory locations are filled with the element. Example: Initially for an array list 10 momory locations are created. After storing 10th element 5 memory location will be added(10/2). after storing 15th element 7 memory locations will be added(15/2 or (10+5)/2).
An Arraylist class object is not synchronized that means multiple threads can work simultanusly on the same resource.


Output: