0% found this document useful (0 votes)
12 views32 pages

Day14 Collections

Java

Uploaded by

mohamedjaaved19
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)
12 views32 pages

Day14 Collections

Java

Uploaded by

mohamedjaaved19
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/ 32

Collections

TNS India Foundation | ‹#›


Partners in Economic Transformation
Recap

● Multithreading- process of executing multiple threads simultaneously.


● Creation of threads- Two ways
One is by extending java.lang.Thread class.
By implementing java.lang.Runnable interface.
● Thread lifecycle - Newborn, Runnable,Running,Blocked and Dead state
● Thread Methods - start(),sleep(),join(),wait(),notify(),notifyAll()
● Each thread has a priority which is a number starts from 1 to 10 .

MIN_PRIORITY =1
NORM_PRIORITY=5
MAX_PRIORITY= 10
TNS India Foundation | ‹#›
Partners in Economic Transformation
Quiz

Q1. What is the purpose of the start() method in java threads?

A It resumes the execution of the thread

B It suspends the execution of the thread

C It starts the execution of the new thread

D It stops the execution of the thread

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. What is the purpose of the start() method in java threads?

A It resumes the execution of the thread

B It suspends the execution of the thread

C It starts the execution of the new thread

D It stops the execution of the thread

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. Which interface is used to create a thread in java?

A Processor

B Executor

C Threadable

D Runnable

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. Which interface is used to create a thread in java?

A Processor

B Executor

C Threadable

D Runnable

TNS India Foundation | ‹#›


Partners in Economic Transformation
Learning Objective

● Comprehend programming collections.


● Explore hierarchy in programming.
● Understand List Interface and its methods.
● Familiarize with ArrayList, LinkedList, Vector.
● Gain knowledge of Iterable interface.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hook

TNS India Foundation | ‹#›


Partners in Economic Transformation
Collection

● A Collection is a group of objects.


● Collections framework provides a set of standard utility classes to manage
collections.
● Collections Framework consists of three parts:
■ Core Interfaces
■ Concrete Implementation
■ Algorithms such as searching and sorting
● A Java Collection is a predefined architecture capable of storing a group of elements
and behaving like a single unit such as an object or a group.
● A collection is an object or container which stores a group of other objects as a
single unit or single entity.
● It is also known as container object or collection object in java.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hierarchy

TNS India Foundation | ‹#›


Partners in Economic Transformation
Collection Interface

TNS India Foundation | ‹#›


Partners in Economic Transformation
List Interface

List Interface represents an ordered or sequential collection of objects.

ArrayList, Vector and LinkedList are some examples of lists.

● Elements of the lists are ordered using Zero based index.


● You can access the elements of lists using an integer index.
● Elements can be inserted at a specific position using integer index.
● Any pre-existing elements at or beyond that position are shifted right.
● Elements can be removed from a specific position. The elements beyond that position
are shifted left.
● A list may contain duplicate elements.
● A list may contain multiple null elements.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Methods of List Interface
● int size(): to get the number of elements in the list.
● boolean isEmpty(): to check if list is empty or not.
● boolean contains(Object o): Returns true if this list contains the specified element.
● Iterator<E> iterator(): Returns an iterator over the elements in this list in proper
sequence.
● Object[] toArray(): Returns an array containing all of the elements in this list in proper
sequence
● boolean add(E e): Appends the specified element to the end of this list.
● boolean remove(Object o): Removes the first occurrence of the specified element
from this list.
● boolean retainAll(Collection<?> c): Retains only the elements in this list that are
contained in the specified collection.
● void clear(): Removes all the elements from the list.
● E get(int index): Returns the element at the specified position in the list.
TNS India Foundation | ‹#›
Partners in Economic Transformation
ArrayList

● ArrayList in Java is a dynamic array that allows us to store multiple objects of any
class or data type.
● It is similar to an array, but there is no fixed size limit.
● ArrayList class is present in the java.util package and is commonly used for storing and
manipulating collections of objects in an arbitrary order.
● It is a resizable array that can grow or shrink in the memory whenever needed. It is
dynamically created with an initial capacity.
● It uses a dynamic array internally for storing the group of elements, objects, or data.
● ArrayList is non-synchronized.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Iterable Interface in Java
● Iterator is an object that enables you to traverse through a collection.
● It can be used to remove elements from the collection selectively, if desired.
● Iterable in Java is an interface that provides the functionality of accessing elements of a
collection one by one.
● Elements of collections like arrays, sets, queues, maps, etc. can be transversed easily
using Iterable.

TNS India Foundation | ‹#›


Partners in Economic Transformation
LinkedList

● It is an implementation of the List and Deque interfaces.


● Internally, it is an implemented using Doubly Linked List Data Structure.
● It supports duplicate elements.
● It stores or maintains it’s elements in Insertion order.
● We can add any number of null elements.
● It is not synchronised that means it is not Thread safe.
● We can create a synchronised LinkedList using Collections.synchronizedList() method.
● In Java applications, we can use it as a List, stack or queue.
● We can use ListIterator to iterate LinkedList elements.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Vector

● Vector implements a dynamic array.


● It is similar to ArrayList, but with two differences −
Vector is synchronized.
Vector contains many legacy methods that are not part of the collections
framework.
● Vector proves to be very useful if you don't know the size of the array in advance or you
just need one that can change sizes over the lifetime of a program

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Difference between arraylist linkedlist vector

TNS India Foundation | ‹#›


Partners in Economic Transformation
Uses of collections

● The collections framework reduces the development time and the burden of
designers, programmers, and users.
● Your code is easier to maintain because it provides useful data structure and
interfaces which reduce programming efforts.
● The size of the container is growable in nature.
● It implements high-performance of useful data structures and algorithms that
increase the performance.
● It enables software reuse.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Real Time Example

● Queues in Ticketing systems.


● TreeMap in Coin changer Application.
● Combination of HashMap and List in Hash Search.
● TreeSet for Binary Search.
● When data dynamically comes from UI or application ( no fixed size defined) then you
must use collections to manage and perform diff operations.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. Which collection allow duplicate elements?

A List

B Set

C Map

D All of the above

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. Which collection allow duplicate elements?

A List

B Set

C Map

D All of the above

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. What is the implementation of List interface?

A HashMap

B HashSet

C LinkedList

D LinkedHashSet

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. What is the implementation of List interface?

A HashMap

B HashSet

C LinkedList

D LinkedHashSet

TNS India Foundation | ‹#›


Partners in Economic Transformation
Summary

● Collection -Group of Objects


● List -Represents an ordered or sequential collection of objects.
● ArrayList -It is a dynamic array that allows us to store multiple objects of any class or
data type.
● LinkedList - Implemented using Doubly Linked List Data Structure
● Vector- Implements a dynamic array and it is synchronized.
● Iterator- To traverse through the elements.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Assignment Question

Problem Statement
You are developing a Student Test Score Tracker application for a school. The application
allows teachers to enter test scores for students and keep track of their performance
throughout the academic year. The provided Java code will be a part of the application to
help teachers input test scores for each student using an array and then display the scores
using an ArrayList from Java Collections.

Input Format
The first line represents the size of array n
The next n space-separated integer represent elements inside array
Output Format
It represents elements inside array

TNS India Foundation | ‹#›


Partners in Economic Transformation
Assignment Question

Sample Input
5
12345
Sample Output
Elements in the ArrayList:
1
2
3
4
5

TNS India Foundation | ‹#›


Partners in Economic Transformation
Let’s Reflect -

1. What is your major learning from today’s session?


2. How are you going to use this learning in your journey on the job?

TNS India Foundation | ‹#›


Partners in Economic Transformation

You might also like