SlideShare a Scribd company logo
Collections
Collection
 Prior to Java 2, Java provided ad hoc classes such
as Dictionary, Vector, Stack, and Properties to
store and manipulate groups of objects.
 Although these classes were quite useful, they
lacked a central, unifying theme.
 Thus, the way that you used Vector was different
from the way that you used Properties.
2
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).
3
Collection Framework
 The collections framework was designed to meet
several goals, such as −
The framework had to be high-performance.
The implementations for the fundamental
collections (dynamic arrays, linked lists, trees,
and hashtables) were to be highly efficient.
The framework had to allow different types of
collections to work in a similar manner and with
a high degree of interoperability.
The framework had to extend and/or adapt a
collection easily.
4
What is a framework in Java?
 It provides readymade architecture.
 It represents a set of classes and interfaces.
 It is optional.
5
Collection Hierarchy
6
Collection Hierarchy
7
 A collections framework is a unified architecture
for representing and manipulating collections. All
collections frameworks contain the following −
Interfaces
Implementations, i.e., Classes
Algorithms
Interfaces
8
 Iterable
 Collection
 List
 Queue
 Deque
 Set
List Interface
9
 The List interface extends Collection and declares
the behavior of a collection that stores a sequence of
elements.
 Elements can be inserted or accessed by their position in the
list, using a zero-based index.
 A list may contain duplicate elements.
 In addition to the methods defined by Collection, List
defines some of its own, which are summarized in the
following table.
 Several of the list methods will throw an
UnsupportedOperationException if the collection cannot be
modified, and a ClassCastException is generated when one
object is incompatible with another.
List Interface Implementations
10
 Stack
 LinkedList
 ArrayList
Stack
11
 Stack is a subclass of Vector that implements a
standard last-in, first-out stack.
 Both Stack and Vector classes are available from
Java 1.0.
 These two classes are called Legacy Classes.
 Both are ThreadSafe with Synchronized Methods.
 Stack only defines the default constructor, which
creates an empty stack. Stack includes all the
methods defined by Vector, and adds several of
its own.
Stack()
Methods in Stack
12
Method Description
boolean empty() Tests if this stack is empty. Returns true
if the stack is empty, and returns false if
the stack contains elements.
Object peek( ) Returns the element on the top of the
stack, but does not remove it.
Object pop( ) Returns the element on the top of the
stack, removing it in the process.
Object push(Object element) Pushes the element onto the stack.
Element is also returned.
int search(Object element) Searches for element in the stack. If
found, its offset from the top of the
stack is returned. Otherwise, -1 is
returned.
Example
13
StackDemo.java
LinkedList
14
 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.
 The important points about Java LinkedList are:
 Java LinkedList class can contain duplicate elements.
 Java LinkedList class maintains insertion order.
 Java LinkedList class is non synchronized.
 In Java LinkedList class, manipulation is fast because no
shifting needs to occur.
 Java LinkedList class can be used as a list, stack or
queue.
LinkedList
15
 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.
 The important points about Java LinkedList are:
 Java LinkedList class can contain duplicate elements.
 Java LinkedList class maintains insertion order.
 Java LinkedList class is non synchronized.
 In Java LinkedList class, manipulation is fast because no
shifting needs to occur.
 Java LinkedList class can be used as a list, stack or
queue.
Methods of LinkedList
16
Method Description
boolean add(E e)
It is used to append the specified
element to the end of a list.
void add(int index, E element)
It is used to insert the specified
element at the specified position
index in a list.
void addFirst(E e)
It is used to insert the given
element at the beginning of a list.
void addLast(E e)
It is used to append the given
element to the end of a list.
boolean contains(Object o)
It is used to return true if a list
contains a specified element.
E element()
It is used to retrieve the first
element of a list.
E get(int index)
It is used to return the element
at the specified position in a list.
Methods of LinkedList
17
Method Description
E get(int index)
It is used to return the element at the specified
position in a list
E getFirst() It is used to return the first element in a list.
E getLast() It is used to return the last element in a list.
int indexOf(Object o)
It is used to return the index in a list of the first
occurrence of the specified element, or -1 if the
list does not contain any element.
int lastIndexOf(Object o)
It is used to return the index in a list of the last
occurrence of the specified element, or -1 if the
list does not contain any element.
E peek() It retrieves the first element of a list
E remove()
It is used to retrieve and removes the first
element of a list.
E removeFirst()
It removes and returns the first element from a
list.
Example
18
LLDemo.java
Arrays
19
 The java.util.Arrays class contains a static
factory that allows arrays to be viewed as lists.
 Following are the important points about Arrays −
This class contains various methods for
manipulating arrays (such as sorting and
searching).
The methods in this class throw a
NullPointerException if the specified array
reference is null.
Example
20
ArraysDemo.java
ArrayList
21
 Java ArrayList class uses a dynamic array for storing
the elements. It inherits AbstractList class and
implements List interface.
 The important points about Java ArrayList class are:
 Java ArrayList class can contain duplicate elements.
 Java ArrayList class maintains insertion order.
 Java ArrayList class is non synchronized.
 Java ArrayList allows random access because array works at
the index basis.
 In Java ArrayList class, manipulation is slow because a lot of
shifting needs to occur if any element is removed from the
array list.
ArrayList
22
 The ArrayList contains the following Constructors
to initialize.
ArrayList()
ArrayList(int capacity)
Example
23
ALDemo.java
Accessing a Collection
24
 To access any Collection, We can use the Iterator
interface.
 An iterator is an interface that is used in place of
Enumerations in the Java Collection Framework.
Moreover, an iterator differs from the
enumerations in two ways:
 Iterator permits the caller to remove the given
elements from the specified collection during the
iteration of the elements.
 Method names have been enhanced.
 Iterator interface is a member connected with
Java Collections Framework.
Methods of Iterator
25
Methods Description
forEachRemaining(Consumer<? super
E>action)
Performs the given action on each of
the element until and unless all the
elements have been processed or
unless an exception is thrown by the
action.
hasNext()
Returns a true value if the more
number of elements are encountered
during iteration.
next()
Returns the next specified element
during the iteration.
remove()
Removes the last element from the
collection as provided by the iterator.

More Related Content

What's hot (20)

PDF
5 collection framework
Minal Maniar
 
PPTX
Java - Collections framework
Riccardo Cardin
 
PPTX
Collections and its types in C# (with examples)
Aijaz Ali Abro
 
PDF
Collections Api - Java
Drishti Bhalla
 
PDF
Java Collection framework
ankitgarg_er
 
PPTX
ArrayList in JAVA
SAGARDAVE29
 
PPTX
Hibernate ppt
Aneega
 
PPT
String Handling
Bharat17485
 
PPTX
Java Strings
RaBiya Chaudhry
 
PDF
Java I/o streams
Hamid Ghorbani
 
PPTX
collection framework in java
MANOJ KUMAR
 
PPT
sets and maps
Rajkattamuri
 
PPT
List in java
nitin kumar
 
PPT
Java package
Arati Gadgil
 
PPT
Java collections concept
kumar gaurav
 
PPT
Collections in Java
Khasim Cise
 
PPTX
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Sagar Verma
 
PPT
JAVA Collections frame work ppt
Ranjith Alappadan
 
PPTX
Java - Generic programming
Riccardo Cardin
 
5 collection framework
Minal Maniar
 
Java - Collections framework
Riccardo Cardin
 
Collections and its types in C# (with examples)
Aijaz Ali Abro
 
Collections Api - Java
Drishti Bhalla
 
Java Collection framework
ankitgarg_er
 
ArrayList in JAVA
SAGARDAVE29
 
Hibernate ppt
Aneega
 
String Handling
Bharat17485
 
Java Strings
RaBiya Chaudhry
 
Java I/o streams
Hamid Ghorbani
 
collection framework in java
MANOJ KUMAR
 
sets and maps
Rajkattamuri
 
List in java
nitin kumar
 
Java package
Arati Gadgil
 
Java collections concept
kumar gaurav
 
Collections in Java
Khasim Cise
 
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Sagar Verma
 
JAVA Collections frame work ppt
Ranjith Alappadan
 
Java - Generic programming
Riccardo Cardin
 

Similar to List interface in collections framework (20)

PPTX
collection framework.pptx
SoniaKapoor56
 
PDF
Array list (java platform se 8 )
charan kumar
 
PDF
Java collections
Hamid Ghorbani
 
PDF
java unit 4 pdf - about java collections
aapalaks
 
PPTX
Advanced Java - UNIT 3.pptx
eyemitra1
 
PPT
DataStructureLists Stacks Queues and more
ssuser69ff25
 
PPT
dataStructure Course about lists stacks queues and more
ssuser69ff25
 
PPT
11000121065_NAITIK CHATTERJEE.ppt
NaitikChatterjee
 
PPTX
Collections lecture 35 40
bhawna sharma
 
PPTX
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
logesswarisrinivasan
 
DOCX
Collections framework
Anand Buddarapu
 
PPTX
Lecture 9
talha ijaz
 
PPT
description of Collections, seaching & Sorting
mdimberu
 
PDF
Java collections
padmad2291
 
DOCX
ArrayList.docx
veerendranath12
 
PPTX
Java.util
Ramakrishna kapa
 
PDF
Collection framework (completenotes) zeeshan
Zeeshan Khan
 
PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
zainmkhan20
 
PPTX
Java Unit 2 (Part 2)
Dr. SURBHI SAROHA
 
PPTX
oop lecture framework,list,maps,collection
ssuseredfbe9
 
collection framework.pptx
SoniaKapoor56
 
Array list (java platform se 8 )
charan kumar
 
Java collections
Hamid Ghorbani
 
java unit 4 pdf - about java collections
aapalaks
 
Advanced Java - UNIT 3.pptx
eyemitra1
 
DataStructureLists Stacks Queues and more
ssuser69ff25
 
dataStructure Course about lists stacks queues and more
ssuser69ff25
 
11000121065_NAITIK CHATTERJEE.ppt
NaitikChatterjee
 
Collections lecture 35 40
bhawna sharma
 
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
logesswarisrinivasan
 
Collections framework
Anand Buddarapu
 
Lecture 9
talha ijaz
 
description of Collections, seaching & Sorting
mdimberu
 
Java collections
padmad2291
 
ArrayList.docx
veerendranath12
 
Java.util
Ramakrishna kapa
 
Collection framework (completenotes) zeeshan
Zeeshan Khan
 
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
zainmkhan20
 
Java Unit 2 (Part 2)
Dr. SURBHI SAROHA
 
oop lecture framework,list,maps,collection
ssuseredfbe9
 
Ad

Recently uploaded (20)

PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
Dimensions of Societal Planning in Commonism
StefanMz
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
community health nursing question paper 2.pdf
Prince kumar
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Ad

List interface in collections framework

  • 2. Collection  Prior to Java 2, Java provided ad hoc classes such as Dictionary, Vector, Stack, and Properties to store and manipulate groups of objects.  Although these classes were quite useful, they lacked a central, unifying theme.  Thus, the way that you used Vector was different from the way that you used Properties. 2
  • 3. 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). 3
  • 4. Collection Framework  The collections framework was designed to meet several goals, such as − The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hashtables) were to be highly efficient. The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability. The framework had to extend and/or adapt a collection easily. 4
  • 5. What is a framework in Java?  It provides readymade architecture.  It represents a set of classes and interfaces.  It is optional. 5
  • 7. Collection Hierarchy 7  A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following − Interfaces Implementations, i.e., Classes Algorithms
  • 8. Interfaces 8  Iterable  Collection  List  Queue  Deque  Set
  • 9. List Interface 9  The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements.  Elements can be inserted or accessed by their position in the list, using a zero-based index.  A list may contain duplicate elements.  In addition to the methods defined by Collection, List defines some of its own, which are summarized in the following table.  Several of the list methods will throw an UnsupportedOperationException if the collection cannot be modified, and a ClassCastException is generated when one object is incompatible with another.
  • 10. List Interface Implementations 10  Stack  LinkedList  ArrayList
  • 11. Stack 11  Stack is a subclass of Vector that implements a standard last-in, first-out stack.  Both Stack and Vector classes are available from Java 1.0.  These two classes are called Legacy Classes.  Both are ThreadSafe with Synchronized Methods.  Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own. Stack()
  • 12. Methods in Stack 12 Method Description boolean empty() Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements. Object peek( ) Returns the element on the top of the stack, but does not remove it. Object pop( ) Returns the element on the top of the stack, removing it in the process. Object push(Object element) Pushes the element onto the stack. Element is also returned. int search(Object element) Searches for element in the stack. If found, its offset from the top of the stack is returned. Otherwise, -1 is returned.
  • 14. LinkedList 14  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.  The important points about Java LinkedList are:  Java LinkedList class can contain duplicate elements.  Java LinkedList class maintains insertion order.  Java LinkedList class is non synchronized.  In Java LinkedList class, manipulation is fast because no shifting needs to occur.  Java LinkedList class can be used as a list, stack or queue.
  • 15. LinkedList 15  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.  The important points about Java LinkedList are:  Java LinkedList class can contain duplicate elements.  Java LinkedList class maintains insertion order.  Java LinkedList class is non synchronized.  In Java LinkedList class, manipulation is fast because no shifting needs to occur.  Java LinkedList class can be used as a list, stack or queue.
  • 16. Methods of LinkedList 16 Method Description boolean add(E e) It is used to append the specified element to the end of a list. void add(int index, E element) It is used to insert the specified element at the specified position index in a list. void addFirst(E e) It is used to insert the given element at the beginning of a list. void addLast(E e) It is used to append the given element to the end of a list. boolean contains(Object o) It is used to return true if a list contains a specified element. E element() It is used to retrieve the first element of a list. E get(int index) It is used to return the element at the specified position in a list.
  • 17. Methods of LinkedList 17 Method Description E get(int index) It is used to return the element at the specified position in a list E getFirst() It is used to return the first element in a list. E getLast() It is used to return the last element in a list. int indexOf(Object o) It is used to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element. int lastIndexOf(Object o) It is used to return the index in a list of the last occurrence of the specified element, or -1 if the list does not contain any element. E peek() It retrieves the first element of a list E remove() It is used to retrieve and removes the first element of a list. E removeFirst() It removes and returns the first element from a list.
  • 19. Arrays 19  The java.util.Arrays class contains a static factory that allows arrays to be viewed as lists.  Following are the important points about Arrays − This class contains various methods for manipulating arrays (such as sorting and searching). The methods in this class throw a NullPointerException if the specified array reference is null.
  • 21. ArrayList 21  Java ArrayList class uses a dynamic array for storing the elements. It inherits AbstractList class and implements List interface.  The important points about Java ArrayList class are:  Java ArrayList class can contain duplicate elements.  Java ArrayList class maintains insertion order.  Java ArrayList class is non synchronized.  Java ArrayList allows random access because array works at the index basis.  In Java ArrayList class, manipulation is slow because a lot of shifting needs to occur if any element is removed from the array list.
  • 22. ArrayList 22  The ArrayList contains the following Constructors to initialize. ArrayList() ArrayList(int capacity)
  • 24. Accessing a Collection 24  To access any Collection, We can use the Iterator interface.  An iterator is an interface that is used in place of Enumerations in the Java Collection Framework. Moreover, an iterator differs from the enumerations in two ways:  Iterator permits the caller to remove the given elements from the specified collection during the iteration of the elements.  Method names have been enhanced.  Iterator interface is a member connected with Java Collections Framework.
  • 25. Methods of Iterator 25 Methods Description forEachRemaining(Consumer<? super E>action) Performs the given action on each of the element until and unless all the elements have been processed or unless an exception is thrown by the action. hasNext() Returns a true value if the more number of elements are encountered during iteration. next() Returns the next specified element during the iteration. remove() Removes the last element from the collection as provided by the iterator.