Java UNIT-5
Java UNIT-5
UNIT – 5
Syllabus
❖ Collection Framework,
❖ Interfaces (Collection, List, Set, Sorted Set, Enumeration,
Iterator, ListIterator),
❖ Classes (LinkedList, ArrayList, Vector, HashSet, TreeSet,
Hashtable),
❖ Working with maps- Map interface,
❖ Map classes(HashMap, TreeMap)
❖ Collection Framework
Java Collections can achieve all the operations that you perform on a data such
as searching, sorting, insertion, manipulation, and deletion.
MANISHA RAJPUT 1
Collections
• Interface: Like a class, an interface can have methods and variables, but
the methods declared in an interface are by default abstract (only
method signature, nobody). Interfaces specify what a class must do and
not how. It is the blueprint of the class.
Collection Interface
The Collection interface is the interface which is implemented by all the classes
in the collection framework. It declares the methods that every collection will
have. In other words, we can say that the Collection interface builds the
foundation on which the collection framework depends.
Some of the methods of Collection interface are Boolean add (Object obj),
Boolean addAll (Collection c), void clear(), etc. which are implemented by all the
subclasses of Collection interface.
MANISHA RAJPUT 2
Collections
List Interface
List interface is the child interface of Collection interface. It inhibits a list type
data structure in which we can store the ordered collection of objects. It can have
duplicate values.
Set Interface
SortedSet
A set is used to provide a particular ordering on its element. The elements are
ordered either by using a natural ordering or by using a Comparator. All the
elements which are inserted into a sorted set must implement the Comparable
interface.
The set's iterator will traverse the set in an ascending order. Several other
operations are provided in order to make best use of ordering. All the elements
must be mutually comparable.
Enumeration
Enumeration is an interface. It is used in the collection framework in java to
retrieve the elements one by one. Enumeration is a legacy interface that is
applicable only for legacy classes like Vector, HashTable, Stack, etc. It
provides a single direction iteration. By using enumeration, we can perform only
read operation, and we cannot perform remove operation.
Enumeration object can be created by calling elements() method present in the
Vector class.
// Here "v" is a vector object. enum is of
MANISHA RAJPUT 3
Collections
Iterators
A Java Cursor is an Iterator, that is used to iterate or traverse or retrieve a
Collection or Stream object’s elements one by one. In this article, we will learn
about Java Iterators and it’s working.
Syntax
Iterator itr = c.iterator();
3. remove(): Removes the next element in the iteration. This method can be
called only once per call to next().
ListIterator
It is only applicable for List collection implemented classes like ArrayList,
LinkedList, etc. It provides bi-directional iteration. ListIterator must be used
when we want to enumerate elements of List. This cursor has more
functionality(methods) than iterator. ListIterator object can be created by
calling listIterator() method present in the List interface.
ArrayList
import java.util.*;
class TestArrayLinked{
public static void main(String args[]){
MANISHA RAJPUT 4
Collections
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
System.out.println("arraylist: "+al);
System.out.println("linkedlist: "+al2);
}
}
Output:
arraylist: [Ravi,Vijay,Ravi,Ajay]
linkedlist: [James,Serena,Swati,Junaid]
LinkedList
Java LinkedList class uses a doubly linked list to store the elements. It provides
a linked-list data structure. It inherits the AbstractList class and implements List
and Deque interfaces.
import java.util.*;
public class LinkedList1{
public static void main(String args[]){
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
MANISHA RAJPUT 5
Collections
Output: Ravi
Vijay
Ravi
Ajay
Vector
Vector is like the dynamic array which can grow or shrink its size. Unlike
array, we can store n-number of elements in it as there is no size limit. It is a
part of Java Collection framework since Java 1.2. It is found in
the java.util package and implements the List interface, so we can use all the
methods of List interface here.
import java.util.*;
public class VectorExample {
public static void main(String args[]) {
//Create a vector
Vector<String> vec = new Vector<String>();
//Adding elements using add() method of List
vec.add("Tiger");
vec.add("Lion");
vec.add("Dog");
vec.add("Elephant");
//Adding elements using addElement() method of Vector
vec.addElement("Rat");
vec.addElement("Cat");
vec.addElement("Deer");
Output:
MANISHA RAJPUT 6