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

Collection Presentations

The document discusses various collection framework classes in Java like Set, List, Queue, Map and their implementations. It explains how to retrieve elements from collections using for-each loop, Iterator, ListIterator and Enumeration interfaces. Specific collection classes like HashSet, ArrayList, Vector and HashMap are described along with their commonly used methods. It provides examples to write programs using these classes.

Uploaded by

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

Collection Presentations

The document discusses various collection framework classes in Java like Set, List, Queue, Map and their implementations. It explains how to retrieve elements from collections using for-each loop, Iterator, ListIterator and Enumeration interfaces. Specific collection classes like HashSet, ArrayList, Vector and HashMap are described along with their commonly used methods. It provides examples to write programs using these classes.

Uploaded by

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

Collection Framework

Objectives

Sets
Lists
Queues
Maps
Retrieving Elements from Collections
Drawbacks of Array

We cannot store different class object into the same array. The
reason is that an array can store one data type of elements

Adding the object at the end of an array is easy. But, inserting and
deleting the elements in the middle of the array is difficult. In this
case, we have to re-arrange all the elements of the array.

Retrieving the elements from an array is easy but after retrieving


the elements, if we want to process, then there are no methods
available to carry out this.

Due this problems, programmers want a better mechanism to


store a group of objects. The alternative is using an object to store
a group of other objects. It means that we can use a class object
as an array. Such an object is called collection object or
container object.
Collection Objects

A collection object or a container object is an object which can store a group


of other objects. A collection object has a class called as collection class or
container classes. All the collection classes are available in the package
java.util. A group of collection classes is called a Collection Framework.

Interface Implementation
Set HashSet
List Stack
LinkedList
ArrayList
Vector
Queue LinkedList
Map HashMap
Hashtable---related to properties
file
Set
A set represents a group of elements arranged just like an array. The
set will grow dynamically when the elements are stored into it. A set
will not allow duplicate elements. If we try to pass the same elements
that is already available in the set, then it is not stored into the set.

Lists
Lists are like sets. They store a group of elements. But lists allow
duplicate values to be stored.
Queues
A Queues represents arrangements of elements in FIFO(First In
First Out) order. This means that an element that is stored as a
First element into the queue will be removed first from the queue.

Maps
Maps store elements in the form of key and value pairs. If the key
is provided then its corresponding value can be obtained. Of
courses, the keys should have unique values.
Retrieving Elements from collections

Using for-each loop


Using Iterator interface
Using ListIterator Interface
Using Enumeration Interface
For-each loop

For-each loop is like for loop which repeatedly executes a group of


statements for each elements of the collection.

The format is:


For(variable: collection-object)
{
Statements;
}
Here , the variable assumes each elements of the collection-object
and the loop
is executed as many times as there are number of elements in the
collections
object. If collection-object has n elements the loop is executed
exactly n times
and the variable stores each element in each step.
Iterator Interface

Iterator is an interface that contains methods to retrieve the


elements one by one from a collections object. It has 3 methods:

Boolean hasNext(): This methods returns true if the Iterator has


more elements

Element next(): This method returns the next element in the


iterator.

Void remove(): This method removes from the collection the last
element returned by the iterator.
ListIterator Interface

ListIterator is an interface that contains methods to retrieve the


elements from a collection object, both in forward and reverse
directions. It has the following important methods.

Boolean hasNext(): This returns true if the ListIterator has more


elements when traversing the list in the forward directions.

Boolean hasPrevious(): This returns true if the ListIterator has


more elements when traversing the list in the reverse directions.

Element next(): This returns the next element in the list.

Element previous(): This returns the previous element in the list.

Void remove(): This removes from the list the last element that
was returned by the next() or previous() methods.
Enumeration Interface

This interface is useful to retrieve one by one the elements like the
Iterator. It has 2 methods.

Boolean hasMoreElements(): This method tests if the Enumeration


has any more elements or not.

Element nextElement(): This returns the next element that is


available in Enumeration.
HashSet Class

A HashSet represents a set of elements (objects). It does not


guarantee the order of elements. Also it does not allow the
duplicate elements to be stored.

The following constructors are available in HashSet:

HashSet()
HashSet(int capacity)
HashSet(int capacity, float loadfactor)
HashSet Class Methods

HashSet class provides the following methods


Boolean add(obj): This method adds an element obj to the HashSet. It
returns true if the element is added to the HashSet, else it returns false. If
the same element is already available in the HashSet, then the present
element is not added.

Boolean remove(obj): This method removes the element obj from the
HashSet, if it is present. It returns true if the element is removed
successfully otherwise false.

Void clear(): This removes all the elements from the HashSet.

Boolean contains(obj): This returns true if the HashSet contains the


specified element obj

Boolean isEmpty(): This returns true if the HashSet contains no Elements.

Int size(): This returns the number of elements present in the HashSet.
See 1st Program

1: write a program which shows the use of HashSet and Iterator


ArrayList

ArrayList is like an array, which can grow in memory dynamically.


It means that when we store elements into the ArrayList,
depending on the number of elements, the memory is dynamically
allotted and re-allotted to accommodate all the elements.

ArrayList is not synchronized.


See 2nd Program

Write a program to create an ArrayList with Strings and perform


various operations on it.
Vector Class

A Vector also stores elements (Objects) similar to ArrayList, but


Vector is synchronized. It means even if several threads act on
vector object simultaneously, the results will be reliable.
See 3rd Program

Write a program that shows the use of Vector Class.


HashMap Class

HashMap is a collection that stores elements in the form of key-


value pairs. If key is provided later, its corresponding value can be
easily retrieved from the HashMap. Keys should be unique. This
means we cannot use duplicate data for keys in the HashMap.
However, HashMap is not synchronized and hence while using
multiple threads on HashMap Object, we get unreliable results.
See 4HashmapExampleth Program

Write a program that shows the use of HashMap class.

You might also like