0% found this document useful (0 votes)
14 views

Java UNIT-5

Thise is the my college notes

Uploaded by

psakshi2129
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java UNIT-5

Thise is the my college notes

Uploaded by

psakshi2129
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Collections

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

The Collection in Java is a framework that provides an architecture to store and


manipulate the group of objects.

Java Collections can achieve all the operations that you perform on a data such
as searching, sorting, insertion, manipulation, and deletion.

Java Collection means a single unit of objects. Java Collection framework


provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList,
Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).

What is a Framework in Java?


A framework is a set of classes and interfaces which provide a ready-made
architecture. In order to implement a new feature or a class, there is no need to
define a framework. However, an optimal object-oriented design always
includes a framework with a collection of classes such that all the classes
perform the same kind of task.

Hierarchy of Collection Framework

MANISHA RAJPUT 1
Collections

Before understanding the different components in the above framework, let’s


first understand a class and an interface.

• Class: A class is a user-defined blueprint or prototype from which


objects are created. It represents the set of properties or methods that are
common to all objects of one type.

• 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.

❖ Interfaces (Collection, List, Set, Sorted Set, Enumeration, Iterator,


ListIterator),

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.

List interface is implemented by the classes ArrayList, LinkedList, Vector, and


Stack.

List Interface declaration


public interface List<E> extends Collection<E>

Set Interface

Set Interface in Java is present in java.util package. It extends the Collection


interface. It represents the unordered set of elements which doesn't allow us to
store the duplicate items. We can store at most one null value in Set. Set is
implemented by HashSet, LinkedHashSet, and TreeSet.

Declaration: The Set interface is declared as:

public interface Set extends Collection

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

// type Enumeration interface and refers to "v"


Enumeration enum = v.elements();

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();

Methods of Iterator Interface in Java


The iterator interface defines three methods as listed below:
1. hasNext(): Returns true if the iteration has more elements.

public boolean hasNext();

2. next(): Returns the next element in the iteration. It


throws NoSuchElementException if no more element is present.

public Object next();

3. remove(): Removes the next element in the iteration. This method can be
called only once per call to next().

public void remove();

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.

❖ Classes (LinkedList, ArrayList, Vector, HashSet, TreeSet, Hashtable)

ArrayList
import java.util.*;
class TestArrayLinked{
public static void main(String args[]){

List<String> al=new ArrayList<String>();//creating arraylist


al.add("Ravi");//adding object in arraylist

MANISHA RAJPUT 4
Collections

al.add("Vijay");
al.add("Ravi");
al.add("Ajay");

List<String> al2=new LinkedList<String>();//creating linkedlist


al2.add("James");//adding object in linkedlist
al2.add("Serena");
al2.add("Swati");
al2.add("Junaid");

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[]){

LinkedList<String> al=new LinkedList<String>();


al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");

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");

System.out.println("Elements are: "+vec);


}
}

Output:

Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]

MANISHA RAJPUT 6

You might also like