0% found this document useful (0 votes)
17 views24 pages

Lecture 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views24 pages

Lecture 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Faculty of Information Technology - Programming 2

Lecture 8
Collection

Spring 2024 61FIT3PR2 – Programming 2 1


What data structure is suitable for storing a large
number of elements?

Spring 2024 61FIT3PR2 – Programming 2 2


Limitations of arrays
• Arrays are useful containers for storing elements of
the same type.
• Arrays lack dynamic allocation and have a fixed
length.
• They are simple linear structures.
• Many applications require more complex data
structures like linked lists, stacks, hash tables, sets,
or trees.

Spring 2024 61FIT3PR2 – Programming 2 3


Collection
• Collection is a data structure used to hold
multiple elements.
• Elements can be added, removed, or updated.
• Operations such as union, intersection,….
difference can be performed on sets.
• Collection is divided into 2 types: List and Set.
• List is a collection where each element is allowed to
appear multiple times and accessed by index.
• Set is a collection where each element is allowed to
appear only once and cannot be accessed by
index.

Spring 2024 61FIT3PR2 – Programming 2 4


Inheritance hierarchy of Collection

Spring 2024 61FIT3PR2 – Programming 2 5


Inheritance hierarchy of Collection

• The entire collection framework is divided into four


interfaces.
1) List
2) Queue
3) Set
4) Map
➢Three of above interfaces (List, Queue and Set) inherit
from Collection interface. Although, Map is included in
collection framework it does not inherit from Collection
interface.

Spring 2024 61FIT3PR2 – Programming 2 6


List
• It handles sequential list of objects. ArrayList, Vector and
LinkedList classes implement this interface.
• The java.util.ArrayList<E> class is the most commonly used
implementation of List<E>.

Spring 2024 61FIT3PR2 – Programming 2 7


Queue
• It handles the special group of objects in which
elements are removed only from the head. LinkedList
and PriorityQueue classes implement this interface.

• Output

Spring 2024 61FIT3PR2 – Programming 2 8


Set
• It handles the group of objects which must contain only
unique elements. This interface is implemented by
HashSet and LinkedHashSet classes and extended
by SortedSet interface which in turn, is implemented
by TreeSet.

• Output

Spring 2024 61FIT3PR2 – Programming 2 9


Map
• This is the one interface in Collection Framework
which is not inherited from Collection interface. It
handles the group of objects as Key/Value pairs. It is
implemented by HashMap and Hashtable classes and
extended by SortedMap interface which in turn is
implemented by TreeMap.

• Output

Spring 2024 61FIT3PR2 – Programming 2 10


Collection API
Method Description
boolean add(Object) Adds an element
addAll(Collection) Adds all element from another collection
boolean remove(Object) Removes the specified element
removeAll(Collection) Removes all elements
retainAll(Collection) Retains only the elements in this collection
that are contained in the specified
collection.
Boolean contains(Object) Checks if a specified element exists
boolean containsAll(Collection) Checks if all elements from a specified
collection exist
int size() Returns the number of elements
boolean isEmpty() Checks if the collection is empty or not.
void clear() Removes all elements
toArray(T[]) Converts the collection to an array.

Spring 2024 61FIT3PR2 – Programming 2 11


List API
• Besides the collection manipulation methods,
List is supplemented with methods that work
with index
Method Description
Object get(int index) retrieves the element at index
Object set(int index, Object elem) Replaces element at index

void add(int index, Object elem) Inserts element at index

Object remove(int index) Removes the element at index


int indexOf(Object elem) Find element position from the
beginning
Int lastIndexOf(Object elem) Find the element position from the end

Spring 2024 61FIT3PR2 – Programming 2 12


List API example

• Output

Spring 2024 61FIT3PR2 – Programming 2 13


Iterate Over a List

Spring 2024 61FIT3PR2 – Programming 2 14


List
• The Collections class provides a set of powerful utility
functions to support List processing

Method Description
int binarySearch(List list, Object key) search for a specified element in a
sorted list using the binary search
algorithm
void fill(List list, Object obj) Assigns values to elements
void shuffle (List list) Random permutation
void sort (List list) Sort in ascending order
void reverse (List list) reverse the order of elements
void rotate (List list, int distance) rotate the elements in the specified
list
void swap(List list, int i, int j) swap the elements at the specified
positions

Spring 2024 61FIT3PR2 – Programming 2 15


Set
• The Set<E> interface models an unordered
mathematical set without duplicate elements.
HashSet<E> is the most common implementation of
Set<E>.

Spring 2024 61FIT3PR2 – Programming 2 16


Sort a set of objects
To sort a set of objects, it is necessary to compare
the objects.
There are two ways to provide criteria for
comparing arguments statue
1. Define comparison criteria in the class by
implementing the Comparable interface and then
writing code to compare 2 objects in the compareTo()
method. This method is rarely used because it is
difficult to change the comparison criteria.
2. Create an object from the Comparator interface then
provide it to the Collections.sort() method. This
method is widely used because of its flexibility in
comparison criteria.
Spring 2024 61FIT3PR2 – Programming 2 17
Way 1: Sort a set of objects

Spring 2024 61FIT3PR2 – Programming 2 18


Way 2: Sort a set of objects

Spring 2024 61FIT3PR2 – Programming 2 19


Map
• Map is a collection of entries.
• Each entry includes a key and value
• Use key to retrieve the value of each element

Spring 2024 61FIT3PR2 – Programming 2 20


Inheritance hierarchy of Map
• HashMap is the most common implementation of
Map

Spring 2024 61FIT3PR2 – Programming 2 21


Map API
Method Description
Object put(Object key, Object Adds or updates an entry
value)
Object get(Object key) Get value according to key
Object remove(Object key) Delete an element by key
boolean containsKey(Object key) Checks for entry existence
according to key
int size() Get the number of entries
boolean isEmpty() Checks whether it is empty or not
void clear() Clear all entries.
Set keySet() Gets the key set
Collection values() Gets the set of values
Set entrySet() Gets the entry set

Spring 2024 61FIT3PR2 – Programming 2 22


Iterate Over a Map

Spring 2024 61FIT3PR2 – Programming 2 23


Summary

• Collection
• Inheritance hierarchy of CollectionConstructor
• List & Array List
• Set & HashSet
• Map & HashMap

Spring 2024 61FIT3PR2 – Programming 2 24

You might also like