SlideShare a Scribd company logo
1 of 25
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

What's hot (20)

Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
Core java
Core java Core java
Core java
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
How Hashmap works internally in java
How Hashmap works internally  in javaHow Hashmap works internally  in java
How Hashmap works internally in java
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
 
07 java collection
07 java collection07 java collection
07 java collection
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
exception handling in java.ppt
exception handling in java.pptexception handling in java.ppt
exception handling in java.ppt
 
Java awt
Java awtJava awt
Java awt
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
 
Java collections
Java collectionsJava collections
Java collections
 

Similar to List interface in collections framework

collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptxSoniaKapoor56
 
Array list (java platform se 8 )
Array list (java platform se 8 )Array list (java platform se 8 )
Array list (java platform se 8 )charan kumar
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxeyemitra1
 
11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.pptNaitikChatterjee
 
Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40bhawna sharma
 
Java collections
Java collectionsJava collections
Java collectionspadmad2291
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanZeeshan Khan
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)SURBHI SAROHA
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionssuseredfbe9
 
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...Sagar Verma
 
The list interface (the java™ tutorials collections interfaces)
The list interface (the java™ tutorials   collections   interfaces)The list interface (the java™ tutorials   collections   interfaces)
The list interface (the java™ tutorials collections interfaces)charan kumar
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & setsRatnaJava
 
Linked list (java platform se 8 )
Linked list (java platform se 8 )Linked list (java platform se 8 )
Linked list (java platform se 8 )charan kumar
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Edureka!
 
Collection framework
Collection frameworkCollection framework
Collection frameworkDilvarSingh2
 

Similar to List interface in collections framework (20)

collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptx
 
Array list (java platform se 8 )
Array list (java platform se 8 )Array list (java platform se 8 )
Array list (java platform se 8 )
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
 
11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt
 
Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40
 
Collections framework
Collections frameworkCollections framework
Collections framework
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Java collections
Java collectionsJava collections
Java collections
 
ArrayList.docx
ArrayList.docxArrayList.docx
ArrayList.docx
 
Java.util
Java.utilJava.util
Java.util
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshan
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
 
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
Collection Framework in Java | Generics | Input-Output in Java | Serializatio...
 
The list interface (the java™ tutorials collections interfaces)
The list interface (the java™ tutorials   collections   interfaces)The list interface (the java™ tutorials   collections   interfaces)
The list interface (the java™ tutorials collections interfaces)
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & sets
 
Linked list (java platform se 8 )
Linked list (java platform se 8 )Linked list (java platform se 8 )
Linked list (java platform se 8 )
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
 
Collection framework
Collection frameworkCollection framework
Collection framework
 

Recently uploaded

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 

Recently uploaded (20)

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 

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.