Java Collections
Collections in java is a framework. A collection allows a "group of objects to be treated as a single unit". These objects can be stored, retrieved, and manipulated as an element of a collection.
Some collection allows duplicate elements and others do not. Some collection are ordered and others unordered.
Some collection allows duplicate elements and others do not. Some collection are ordered and others unordered.
What is Framework ?
- Framework provides readymade architecture.
- Framework represents set of classes and interfaces.
What is Collection
Collection represents the group of objects as a single unit. Java collection framework introduced in J2SE 1.2 released. Collections are set of classes and interfaces.
By using collection, You can perform operations on data such as insertion, deletion, searching, sorting and manipulation easily. The collection is an alternate of a java array.
What is Collection Framework in Java
Collection framework in java is a unified architecture for storing and manipulating collections i.e group of objects. Java collection framework contains :
- Interfaces
- Interfaces implementer classes
- Algorithm
Collection framework basically used to handle the data structure in java programming language.
Java collections framework provides many interfaces and classes, these are :
Package :
java.util package
Classes :
Java collections framework provides many interfaces and classes, these are :
Package :
java.util package
Classes :
- ArrayList
- LinkedList
- Vector
- HashSet
- LinkedHashSet
- TreeSet
- PriorityQueue
Interfaces :
- Collection
- List
- Set
- Queue
- Dequeue
Hierarchy of Collection Framework
The java.util package contains all the classes and interfaces for the collection framework in java. When we wil make any collection program, we have to import java.util package in that program.
Collection Interface Methods
There are many useful methods of collection interface in java.
(1) public boolean add(Object element)
This method is used to insert an element in the collection.
(2) public boolean addAll(Collection c)
This method is used to insert the specified collection elements in the invoking collection.
(3) public boolean remove(Object element)
This method is used to remove or delete an element from this collection.
(4) public boolean removeAll(Collection c)
This method is used to remove or delete all the elements of specified collection from the invoking collection.
(5) public boolean retainAll(Collection c)
This method is used to delete all the elements of invoking collection except the specified collection.
(6) public int size()
This is method is used to return the total number of elements in the collection.
(7) public void clear()
This method is used to remove the total number of elements in the collection.
(8) public boolean contains(Object element)
This method is used to search an element in the collection.
(9) public boolean containsAll(Collection c)
This method is used to search the specified collection in the collection.
(10) public Iterator iterator()
returns an iterator.
(11) public Object[] toArray()
This is used to converts collection into array.
(12) public boolean isEmpty()
This is used to find collection is empty or not.
(13) public boolean equals(Object element)
This is used to matches two collection.
(14) public int hashCode()
return the hashcode number for collection.
Visit for further topics like:
Difference Between HashSet and HashMap in Collection.
Difference Between ArrayList and LinkedList in Collection.
(1) public boolean add(Object element)
This method is used to insert an element in the collection.
(2) public boolean addAll(Collection c)
This method is used to insert the specified collection elements in the invoking collection.
This method is used to remove or delete an element from this collection.
(4) public boolean removeAll(Collection c)
This method is used to remove or delete all the elements of specified collection from the invoking collection.
(5) public boolean retainAll(Collection c)
This method is used to delete all the elements of invoking collection except the specified collection.
(6) public int size()
This is method is used to return the total number of elements in the collection.
(7) public void clear()
This method is used to remove the total number of elements in the collection.
(8) public boolean contains(Object element)
This method is used to search an element in the collection.
(9) public boolean containsAll(Collection c)
This method is used to search the specified collection in the collection.
(10) public Iterator iterator()
returns an iterator.
(11) public Object[] toArray()
This is used to converts collection into array.
(12) public boolean isEmpty()
This is used to find collection is empty or not.
(13) public boolean equals(Object element)
This is used to matches two collection.
(14) public int hashCode()
return the hashcode number for collection.
Visit for further topics like:
Difference Between HashSet and HashMap in Collection.
Difference Between ArrayList and LinkedList in Collection.
Iterator Java
Iterator is an interface in java collection Framework. Iterator interface is basically used to iterate or draw an element in the collection in the forward direction only.Iterator methods
There are three methods in the iterator interface, these are :
- public boolean hasNext() : It returns true if iterator has more elements.
- public object next() : It returns the elements and moves cursor pointer to the next element.
- public void remove() : It removes the last elements returned by the iterator.
Read More: