SlideShare a Scribd company logo
Java/J2EE Programming Training
Collections
Page 2Classification: Restricted
Agenda
• Recap of Arrays
• Introduction to Collections API
• Lists – ArrayList, Vector, LinkedList
Arrays…
Classification: Restricted Page 3
• How are arrays defined?
int[] myArray = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
int[] myArray = new int[10];
myArray[0] = 2;
myArray[1] = 5; …
• Iterating through array: Use for loop
• Sort and search: Use java.util.Arrays
• Demo of arrays…
Major shortcoming of arrays?
Classification: Restricted Page 4
• Length of arrays is fixed when the array is created. It cannot be changed
after that.
• The solution is to use one of the List classes from the Collections API.
• PROGRAM = DATA STRUCTURE + ALGORITHM;
Readings and References
Classification: Restricted Page 6
• References
• "Collections", Java tutorial
• http://java.sun.com/docs/books/tutorial/collections/index.html
Collections Framework
Classification: Restricted Page 7
• Unified architecture for representing and manipulating
collections.
• A collections framework contains three things
• Interfaces
• Implementations
• Algorithms
Collections Framework Diagram
Classification: Restricted Page 8
Collection Interface
Classification: Restricted Page 9
• Defines fundamental methods
•int size();
•boolean isEmpty();
•boolean contains(Object element);
•boolean add(Object element);
•boolean remove(Object element);
•Iterator iterator();
• These methods are enough to define the basic behavior of a
collection
• Provides an Iterator to step through the elements in the Collection
Iterator Interface
Classification: Restricted Page 10
• Defines three fundamental methods
•Object next()
•boolean hasNext()
•void remove()
• These three methods provide access to the contents of the
collection
• An Iterator knows position within collection
• Each call to next() “reads” an element from the collection
•Then you can use it or remove it
Iterator Position
Classification: Restricted Page 11
Example - SimpleCollection
Classification: Restricted Page 12
public class SimpleCollection {
public static void main(String[] args) {
Collection c;
c = new ArrayList();
System.out.println(c.getClass().getName()); for (int
i=1; i <= 10; i++) {
c.add(i + " * " + i + " = "+i*i);
}
Iterator iter = c.iterator(); while
(iter.hasNext())
System.out.println(iter.next());
}
}
List Interface Context
Classification: Restricted Page 13
Collection
List
ArrayList and LinkedList Context
ArrayList LinkedList
Classification: Restricted Page 14
Collection
List
List as part of Collection
Classification: Restricted Page 15
List Implementations
Classification: Restricted Page 16
• ArrayList
• low cost random access
• high cost insert and delete
• array that resizes if need be
• LinkedList
• sequential access
• low cost insert and delete
• high cost random access
• Vector
• Similar to ArrayList, but thread-safe
ArrayList overview
Classification: Restricted Page 17
• Constant time positional access (it’s an array)
• One tuning parameter, the initial capacity
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException(
"Illegal Capacity: "+initialCapacity);
this.elementData = new Object[initialCapacity];
}
ArrayList methods
Classification: Restricted Page 18
• The indexed get and set methods of the List interface are
appropriate to use since ArrayLists are backed by an array
•Object get(int index)
•Object set(int index, Object element)
• Indexed add and remove are provided, but can be costly if used
frequently
•void add(int index, Object element)
•Object remove(int index)
• May want to resize in one shot if adding many elements
•void ensureCapacity(int minCapacity)
Example: ArrayList
Classification: Restricted Page 19
ArrayList al = new ArrayList();
al.add(3);
al.add(2);
al.add(1);
al.add(4);
al.add(5);
al.add(6);
al.add(6);
Iterator iter1 = al.iterator();
while(iter1.hasNext()){
System.out.println(iter1.next());
}
Example: ArrayList (Using Generics + Iterating using Iterator
and for-each loop)
Classification: Restricted Page 20
import java.util.*; class
TestCollection1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add("Ravi");//Adding object in arraylist
list.add("Vijay");
list.add("Ravi");
list.add("Ajay");
//Traversing list through Iterator
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
//Traversing using for-each loop
for(String obj:list)
System.out.println(obj);
}
User-defined class objects in Java ArrayList
Classification: Restricted Page 21
class Student{
int rollno;
String
name; int
age;
Student(int rollno,String name,int
age){ this.rollno=rollno;
this.name=name
; this.age=age;
}
}
import java.util.*;
public class TestCollection3{
public static void main(String args[]){
//Creating user-defined class objects Student
s1=new Student(101,"Sonoo",23); Student s2=new
Student(102,"Ravi",21);
Student s3=new Student(103,"Hanumat",25);
//creating arraylist
ArrayList<Student> al=new ArrayList<Student>();
al.add(s1);//adding Student class object al.add(s2);
al.add(s3);
//Getting Iterator Iterator
itr=al.iterator();
//traversing elements of ArrayList object
while(itr.hasNext()){
Student st=(Student)itr.next();
System.out.println(st.rollno+" "+st.name+"
"+st.age);
}
}
Thank You

More Related Content

PPSX
Collections - Lists, Sets
PPSX
Collections - Maps
PPTX
Session 17 - Collections - Lists, Sets
PPSX
Collections - Array List
PPTX
Session 15 - Collections - Array List
PPTX
Session 20 - Collections - Maps
PDF
Java Collection framework
PPT
L11 array list
Collections - Lists, Sets
Collections - Maps
Session 17 - Collections - Lists, Sets
Collections - Array List
Session 15 - Collections - Array List
Session 20 - Collections - Maps
Java Collection framework
L11 array list

What's hot (20)

PPSX
Object Class
PPTX
Java Collections Framework Inroduction with Video Tutorial
PPT
Java collection
PPTX
Session 14 - Object Class
PPTX
Collections framework in java
PPT
Collection Framework in java
PPTX
Array vs array list
PPTX
Collections - Lists & sets
PDF
5 collection framework
PPT
Java Collections Framework
PDF
Collections in Java Notes
PDF
The map interface (the java™ tutorials collections interfaces)
PPTX
Java - Collections framework
PPT
JAVA Collections frame work ppt
PPTX
C# Non generics collection
PDF
Java ArrayList Tutorial | Edureka
PPTX
collection framework in java
PPSX
Collections - Sorting, Comparing Basics
PPTX
Java collections
DOCX
Java collections notes
Object Class
Java Collections Framework Inroduction with Video Tutorial
Java collection
Session 14 - Object Class
Collections framework in java
Collection Framework in java
Array vs array list
Collections - Lists & sets
5 collection framework
Java Collections Framework
Collections in Java Notes
The map interface (the java™ tutorials collections interfaces)
Java - Collections framework
JAVA Collections frame work ppt
C# Non generics collection
Java ArrayList Tutorial | Edureka
collection framework in java
Collections - Sorting, Comparing Basics
Java collections
Java collections notes
Ad

Similar to Collections Array list (20)

PPTX
Updated_Java_Collections_Framework_Presentation.pptx
PPTX
Java_Collections_Framework_Presentation (1).pptx
PPTX
Collection framework
PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
PPTX
Collections Training
PPTX
collection framework.pptx
PPTX
Java Collection
PDF
CF5_Unit4.ppt.pdf java collection frameworks
PPT
12_-_Collections_Framework
PPTX
VTUOOPMCA5THMODULECollection OverV .pptx
PPTX
mca5thCollection OverViCollection O.pptx
PPTX
VTUOOPMCA5THMODULECollection OverVi.pptx
PPTX
VTUOOPMCA5THMODULEvCollection OverV.pptx
PDF
Lecture 8_٠٨٣٣٣٦taiz unvercity object oreinted programming.pdf
PDF
Collections In Java
PDF
Collections in java
PDF
Collections
PPTX
Java util
PPTX
collectionsframework210616084411 (1).pptx
Updated_Java_Collections_Framework_Presentation.pptx
Java_Collections_Framework_Presentation (1).pptx
Collection framework
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
Collections Training
collection framework.pptx
Java Collection
CF5_Unit4.ppt.pdf java collection frameworks
12_-_Collections_Framework
VTUOOPMCA5THMODULECollection OverV .pptx
mca5thCollection OverViCollection O.pptx
VTUOOPMCA5THMODULECollection OverVi.pptx
VTUOOPMCA5THMODULEvCollection OverV.pptx
Lecture 8_٠٨٣٣٣٦taiz unvercity object oreinted programming.pdf
Collections In Java
Collections in java
Collections
Java util
collectionsframework210616084411 (1).pptx
Ad

More from RatnaJava (13)

PPTX
Review Session and Attending Java Interviews
PPTX
Collections - Sorting, Comparing Basics
PPTX
Object Class
PPTX
Exception Handling
PPTX
OOPs with Java - Packaging and Access Modifiers
PPTX
OOP with Java - Abstract Classes and Interfaces
PPTX
OOP with Java - Part 3
PPTX
OOP with Java - continued
PPTX
Object Oriented Programming
PPTX
Data Handling and Function
PPTX
Introduction to Java Part-3
PPTX
Introduction to Java Part-2
PPTX
Introduction to Java
Review Session and Attending Java Interviews
Collections - Sorting, Comparing Basics
Object Class
Exception Handling
OOPs with Java - Packaging and Access Modifiers
OOP with Java - Abstract Classes and Interfaces
OOP with Java - Part 3
OOP with Java - continued
Object Oriented Programming
Data Handling and Function
Introduction to Java Part-3
Introduction to Java Part-2
Introduction to Java

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Approach and Philosophy of On baking technology
PPTX
Cloud computing and distributed systems.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation theory and applications.pdf
PPT
Teaching material agriculture food technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Programs and apps: productivity, graphics, security and other tools
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
MYSQL Presentation for SQL database connectivity
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Electronic commerce courselecture one. Pdf
Empathic Computing: Creating Shared Understanding
Approach and Philosophy of On baking technology
Cloud computing and distributed systems.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Chapter 3 Spatial Domain Image Processing.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Network Security Unit 5.pdf for BCA BBA.
Unlocking AI with Model Context Protocol (MCP)
Encapsulation theory and applications.pdf
Teaching material agriculture food technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
The AUB Centre for AI in Media Proposal.docx
Programs and apps: productivity, graphics, security and other tools
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
MYSQL Presentation for SQL database connectivity

Collections Array list

  • 2. Page 2Classification: Restricted Agenda • Recap of Arrays • Introduction to Collections API • Lists – ArrayList, Vector, LinkedList
  • 3. Arrays… Classification: Restricted Page 3 • How are arrays defined? int[] myArray = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 }; int[] myArray = new int[10]; myArray[0] = 2; myArray[1] = 5; … • Iterating through array: Use for loop • Sort and search: Use java.util.Arrays • Demo of arrays…
  • 4. Major shortcoming of arrays? Classification: Restricted Page 4 • Length of arrays is fixed when the array is created. It cannot be changed after that. • The solution is to use one of the List classes from the Collections API. • PROGRAM = DATA STRUCTURE + ALGORITHM;
  • 5. Readings and References Classification: Restricted Page 6 • References • "Collections", Java tutorial • http://java.sun.com/docs/books/tutorial/collections/index.html
  • 6. Collections Framework Classification: Restricted Page 7 • Unified architecture for representing and manipulating collections. • A collections framework contains three things • Interfaces • Implementations • Algorithms
  • 8. Collection Interface Classification: Restricted Page 9 • Defines fundamental methods •int size(); •boolean isEmpty(); •boolean contains(Object element); •boolean add(Object element); •boolean remove(Object element); •Iterator iterator(); • These methods are enough to define the basic behavior of a collection • Provides an Iterator to step through the elements in the Collection
  • 9. Iterator Interface Classification: Restricted Page 10 • Defines three fundamental methods •Object next() •boolean hasNext() •void remove() • These three methods provide access to the contents of the collection • An Iterator knows position within collection • Each call to next() “reads” an element from the collection •Then you can use it or remove it
  • 11. Example - SimpleCollection Classification: Restricted Page 12 public class SimpleCollection { public static void main(String[] args) { Collection c; c = new ArrayList(); System.out.println(c.getClass().getName()); for (int i=1; i <= 10; i++) { c.add(i + " * " + i + " = "+i*i); } Iterator iter = c.iterator(); while (iter.hasNext()) System.out.println(iter.next()); } }
  • 12. List Interface Context Classification: Restricted Page 13 Collection List
  • 13. ArrayList and LinkedList Context ArrayList LinkedList Classification: Restricted Page 14 Collection List
  • 14. List as part of Collection Classification: Restricted Page 15
  • 15. List Implementations Classification: Restricted Page 16 • ArrayList • low cost random access • high cost insert and delete • array that resizes if need be • LinkedList • sequential access • low cost insert and delete • high cost random access • Vector • Similar to ArrayList, but thread-safe
  • 16. ArrayList overview Classification: Restricted Page 17 • Constant time positional access (it’s an array) • One tuning parameter, the initial capacity public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException( "Illegal Capacity: "+initialCapacity); this.elementData = new Object[initialCapacity]; }
  • 17. ArrayList methods Classification: Restricted Page 18 • The indexed get and set methods of the List interface are appropriate to use since ArrayLists are backed by an array •Object get(int index) •Object set(int index, Object element) • Indexed add and remove are provided, but can be costly if used frequently •void add(int index, Object element) •Object remove(int index) • May want to resize in one shot if adding many elements •void ensureCapacity(int minCapacity)
  • 18. Example: ArrayList Classification: Restricted Page 19 ArrayList al = new ArrayList(); al.add(3); al.add(2); al.add(1); al.add(4); al.add(5); al.add(6); al.add(6); Iterator iter1 = al.iterator(); while(iter1.hasNext()){ System.out.println(iter1.next()); }
  • 19. Example: ArrayList (Using Generics + Iterating using Iterator and for-each loop) Classification: Restricted Page 20 import java.util.*; class TestCollection1{ public static void main(String args[]){ ArrayList<String> list=new ArrayList<String>();//Creating arraylist list.add("Ravi");//Adding object in arraylist list.add("Vijay"); list.add("Ravi"); list.add("Ajay"); //Traversing list through Iterator Iterator itr=list.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } //Traversing using for-each loop for(String obj:list) System.out.println(obj); }
  • 20. User-defined class objects in Java ArrayList Classification: Restricted Page 21 class Student{ int rollno; String name; int age; Student(int rollno,String name,int age){ this.rollno=rollno; this.name=name ; this.age=age; } } import java.util.*; public class TestCollection3{ public static void main(String args[]){ //Creating user-defined class objects Student s1=new Student(101,"Sonoo",23); Student s2=new Student(102,"Ravi",21); Student s3=new Student(103,"Hanumat",25); //creating arraylist ArrayList<Student> al=new ArrayList<Student>(); al.add(s1);//adding Student class object al.add(s2); al.add(s3); //Getting Iterator Iterator itr=al.iterator(); //traversing elements of ArrayList object while(itr.hasNext()){ Student st=(Student)itr.next(); System.out.println(st.rollno+" "+st.name+" "+st.age); } }