Share this post on:

Collection Framework in Java is use for provide unified model to work with different type of data structure such as array, binary tree, hash table, etc. At the core of collection framework is and Interface name java.util.collection. collection is a predefined interface at core collection process.

Methods in Collection Framework in Java

add() : It is used to add an element into collection.

syntax: public boolean add(object element);

addAll() : It is used to add all the elements of one collection into another collection.

syntax: public boolean addAll(collection e);

remove() : It is used to remove an element from an Collection.

syntax: public boolean remove(object element);

removeAll() : It is used to remove all the element from an Collection.

syntax: public boolean removeAll(object element);

retainAll() : It is used to remove all the elements which are not in another collection.

syntax: public boolean retainAll(object element);

clear() : It is used to clear the collection

size() : It is used to obtain the size of collection

syntax: public int size();

contain() : It is used to search an elements from collection.

syntax: public boolean contain(object element);

iterator() : It is used for traversing the collection. It return base address of collection.

syntax: public Iterator iterator();

Important method of Iterator :-

  • hashNext() : It is used for checking is there any elements or not for traversing.
  • next() : It is used for obtaining address of next element to be traverse.

List in Collection Framework in Java

List is an Interface which extends collection and ListInterface by default. List interface is also called as index based collection. List interface allow based duplicacy that is we can insert duplicate elements in list collection.

ArrayList and LinkedList classes are used for the implementation of list collection. List indexing must be start with 0.

Methods of List Collection :-

  • add() : It is used to add an element into the list according to index.
  • remove() : It is used to remove an element from a list based on index.
  • listIterator() : It is used to take last index address of collection.

More Methods in Collection :-

  • hasPrevious() : It is used to check is there an elements or not for traversing.
  • previous() : It is used for obtaining the address of desired elements to be traverse.

Example: Write Program to demonstrate List Interface Collection?

import java.util.ArrayList;
import java.util.Iterator;

public class Practice
{
public static void main(String[] args)
{
ArrayList list = new ArrayList();
list.add(“List1”);
list.add(“List2”);
list.add(1,”List3″);
list.add(“List4”);
list.add(“List5”);
System.out.println(“Total size of collection “+list.size());
System.out.println(list.contains(“List5”));
System.out.println(list.contains(“List2”));
Iterator iterator= list.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
}
}

Output:

Total size of collection 5
true
true
List1
List3
List2
List4
List5

Set in Collection Framework in Java

Set is a Interface extends Collection Interface by default. set is non Index based Collection. Set Interface collection does not allows Duplicacy that is we can’t insert duplicate elements in a set Collection.

HashSet and TreeSet Class are used for the implementation of set Collection. Both have their own method or technique for storing & traversing of the elements.

HashSet have unique process to store or retrieve of elements in set collection. where as TreeSet allows Binary search Tree algorithm’s that is inorder Traversal.

Write a Program to Demonstrate TreeSet ?

import java.util.Iterator;
import java.util.TreeSet;

public class Practice
{
public static void main(String[] args)
{
TreeSet set = new TreeSet();

set.add(“Data 1”);
set.add(“Data 2”);
set.add(“Data 3”);
set.add(“Data 4”);
set.add(“Data 5”);
set.add(“Data 1”);
Iterator iterator=set.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
}

}

Output:

Data 1
Data 2
Data 3
Data 4
Data 5

Write a Program to Demonstrate HashSet?

import java.util.HashSet;
import java.util.Iterator;

public class Practice
{
public static void main(String[] args)
{
HashSet set = new HashSet();

set.add(“Data 1”);
set.add(“Data 2”);
set.add(“Data 3”);
set.add(“Data 4”);
set.add(“Data 5”);
set.add(“Data 1”);
Iterator iterator=set.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
}

}

Output:

Data 3
Data 2
Data 5
Data 4
Data 1

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

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