SlideShare a Scribd company logo
Java collections
The List Interface
 Method: void add(int index, Object o)
 Inserts specified element at specified position
Example:
list.add(4, a); //adds element a at position 4
The List Interface
 Method: boolean add(Object o)
 Adds specified element to the end of the list
Example:
list.add(a); //adds element a at the end of the list
The List Interface
 Method: boolean contains(Object 0)
 Returns true if the specified element is in the list
Example:
list.add(a);
list.contains(a); //returns true
////////////////////////////////////////////////////////////
list.add(b);
list.contains(a); //returns false
The List Interface
 Method: Object get(int index)
 Returns the element at the specified position
Example:
list.add(a);
list.add(b);
list.get(1); //returns b
The List Interface
 Method: int indexOf(object o);
 Returns the index of the specified element or else -1
Example:
list.add(a);
list.add(b);
list.add(c);
list.indexOf(c); //returns 2
The List Interface
 Method: int lastIndexOf(object o);
 Returns the last index of the specified element or
else -1
Example:
list.add(a);
list.add(b);
list.add(a);
list.indexOf(a); //returns 2
The List Interface
 Method: Object remove(int index)
 Removes the element at the specified index
Example:
list.add(a);
list.add(b);
list.add(c);
list.remove(1); //removes element a
The List Interface
 Method: boolean remove(Object o)
 Removes the first occurrence of the specified element
Example:
list.add(a);
list.add(b);
list.add(a);
list.remove(a); //removes element a at position 0
The List Interface
 Method: object set(int index, Object element)
 Replaces the element at the specified position and
returns the old element
Example:
list.add(a);
list.set(0, b); //replaces element a with b and
returns element a
The List Interface
 Method: boolean isEmpty()
 Returns true if the method is empty
Example
list.isEmpty(); //retruns true
////////////////////////////////////////////////////////////
list.add(a);
list.isEmpty(); //returns false
The List Interface
 Method: int size()
 Returns the number of elements in the list
Example:
list.add(a);
list.add(b);
list.add(c);
list.size(); //Returns 3
The List Interface
 Method: boolean addAll(Collection c)
 Appends all the elements in the specified collection to
the end of the list
Example:
list2.add(a);
list2.add(b);
list2.add(c);
list.addAll(list); //adds all elements in list2 to
list
The List Interface
 Method: boolean addAll(int index, Collection c)
 Appends all the elements in the specified collection to
the specified position
Example:
list2.add(a);
list2.add(b);
list.add(a);
list.addAll(0,list); //adds all elements in list2 to
list before element a
The List Interface
 Method: void clear()
 Removes all elements from the list
Example:
list.add(a);
list.add(b);
list.clear(); //removes elements a and b
The List Interface
 Methods: boolean conatainsAll(Collection c)
 Returns true if the list contains all the element in the
collection
Example:
list2.add(a);
list2.add(b);
list.add(a);
list.add(b);
list2.containAll(list); //Returns true
The List Interface
 Method: equals(Object o)
 Compares the specific object with the list for equality
 Example
list.add(a);
list2.add(a);
list.equals(list2); //Returns true
///////////////////////////////////////////////////////////////////////////////
list.add(a);
list2.add(b);
list.equals(list2); //Returns false
The List Interface
 Method: Iterator iterator()
 Returns an iterator over the elements in the list in
proper sequence
 Example
Iterator iter = list.iterator(); //Creates an
iterator for list
The List Interface
 Method: ListIterator listIterator()
 Returns a list iterator over the elements in the list in
proper sequence
Example
ListIterator iter = list.listIterator(); //Creates a
list iterator
The List Interface
 Method: ListIterator listIterator(int index)
 Returns a list iterator over the elements in the list in
proper sequence, starting at the indicated position
Example
ListIterator iter = list.listIterator(2); //Creates
a list iterator starting at 2
The List Interface
 Method: boolean removeAll(collection c)
 Removes all of the elements specified from the list
Example
list2.add(a);
list2.add(b);
list2.add(c);
list.add(a);
list.add(b);
list2.removeAll(list); //Removes elements a and
b
The List Interface
 Method: boolean retainAll(collection c)
 Retains only the elements specified in the list
Example
list2.add(a);
list2.add(b);
list2.add(c);
list.add(a);
list2.retainAll(list); //Removes elements b and c
and keeps a
The List Interface
 Method: List subList(int fromIndex, int toIndex)
 Returns a view of the portion of the list between the
specified indexes
Example
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.subList(1,3); //returns a view of elements
b,c,d
The List Interface
 Method: Object[] toArray()
 Returns an array containg all of the elements in the
list
Example:
list.add(1);
list.add(2);
list.add(3);
int[] array = list.toArray(); //Creates an array
conating all of lists elements: 1, 2, and 3
The List Interface
 Method: Object[] toArray(Object a)
 Returns an array containg all of the elements in the
list, witht the runtime/ size of the array according to
the object
Example:
list.add(1);
list.add(2);
list.add(3);
int[] array = list.toArray(list); //Creats an array
conating all of lists elements: 1, 2, and 3 with size of list
Iterator Interface
 Method: boolean hasNext()
 Returns true if there are any items in the next position
Example:
While(iter.hasNext()) //Checks if there is a next
position
iter.next();
Iterator Interface
 Method: Object next()
 Returns the next item and moves to the next position
if hasNext is true
Example:
While(iter.hasNext())
iter.next(); //moves the position of the
array to the next spot
Iterator Interface
 Method: void remove()
 Removes item returned by the most recent call of next
Example:
While(iter.hasNext())
iter.next();
iter.remove(); //clears the list
Iterator Interface
 Method: boolean hasPrevious()
 Returns true if there are any items in the previous
position
Example:
While(iter.hasPrevious()) //Checks if there is a
previous position
iter.previous();
Iterator Interface
 Method: Object previous()
 Returns the previous item and moves to the position
before the current location if hasPrevious is true
Example:
While(iter.hasPrevious())
iter.previous(); //moves the position of
the array to the previous spot
Iterator Interface
 Method: Object nextIndex()
 Returns the value of the next index or -1 if none
Example:
list.add(a);
list.add(b);
ListIterator iter = list.listIterator();
iter.nextIndex(); //returns b
/////////////////////////////////////////////////////////////////
list.add(a);
ListIterator iter = list.listIterator();
iter.nextIndex(); //returns -1
Iterator Interface
 Method: Object previousIndex()
 Returns the value of the previous index or -1 if none
Example:
list.add(a);
ListIterator iter = list.listIterator();
iter.previousIndex(); //returns -1
/////////////////////////////////////////////////////////////////
list.add(a);
list.add(b);
iter.next();
ListIterator iter = list.listIterator();
iter.previousIndex(); //returns a
Iterator Interface
 Method: void add(Object 0)
 Inserts an object o at the current pposition
Example:
list.add(a);
list.add(b);
ListIterator iter = list.listIterator();
iter.add(z); // list looks like (z,a,b)
Iterator Interface
 Method: void set(Object o)
 Replaces the last item returned by next or previous
Example:
list.add(a);
list.add(b);
ListIterator iter = list.listIterator();
iter.next();
iter.set(z); //List looks like (a,z)

More Related Content

PPTX
F sharp lists & dictionary
PPTX
poornima.coseq
PPTX
Kotlin For Android - Collections APIs (part 6 of 7)
PPT
Chapter14
PPT
강의자료10
PPT
computer notes - Data Structures - 2
PPTX
Linear and binary search
F sharp lists & dictionary
poornima.coseq
Kotlin For Android - Collections APIs (part 6 of 7)
Chapter14
강의자료10
computer notes - Data Structures - 2
Linear and binary search

What's hot (12)

DOCX
Bubble sorting lab manual
PPTX
Lists
PDF
Problem Solving with Algorithms and Data Structure - Lists
PPT
Engineering lecture ppt by venay magen
PPTX
Vector list nd sequence
PPSX
Data Structure (Stack)
PPTX
Stacks in DATA STRUCTURE
PDF
stacks and queues
PPT
Stack linked list
PPT
Stack Implementation
PPT
Position
PPTX
Stacks in Data Structure
Bubble sorting lab manual
Lists
Problem Solving with Algorithms and Data Structure - Lists
Engineering lecture ppt by venay magen
Vector list nd sequence
Data Structure (Stack)
Stacks in DATA STRUCTURE
stacks and queues
Stack linked list
Stack Implementation
Position
Stacks in Data Structure
Ad

Viewers also liked (18)

PDF
Vivametrica Media Coverage_2014
DOCX
AAR - Papal Visit 2015
PDF
Zintegrowane systemy zarządzania lech 11
PDF
Performance Knowledge-Based Official Development Assistance Framework
PPT
Reference materials (3)
PPTX
Using the I in BIM for Interiors - CSRW2013
TXT
Confidencias reales
DOCX
MSFP_C&KMS-FINAL_DRAFT_01.12.2015
PPT
Moral dev 1 (1)
PDF
CVnewshortest
PDF
nama ella azhari, nim: 140601009
PPTX
PDF
Glossary of Pumping terms + Pumps Industry Terminology
PPTX
Question 4 - How did you use media technologies in the construction and resea...
PPTX
Media Gambar Sholat Jum'at
PPTX
Resort-SS 16 MGF Print Trends-compressed
PDF
2001_No1_Thinking_peace_making_peace
PDF
How to use the Company Connecting Search
Vivametrica Media Coverage_2014
AAR - Papal Visit 2015
Zintegrowane systemy zarządzania lech 11
Performance Knowledge-Based Official Development Assistance Framework
Reference materials (3)
Using the I in BIM for Interiors - CSRW2013
Confidencias reales
MSFP_C&KMS-FINAL_DRAFT_01.12.2015
Moral dev 1 (1)
CVnewshortest
nama ella azhari, nim: 140601009
Glossary of Pumping terms + Pumps Industry Terminology
Question 4 - How did you use media technologies in the construction and resea...
Media Gambar Sholat Jum'at
Resort-SS 16 MGF Print Trends-compressed
2001_No1_Thinking_peace_making_peace
How to use the Company Connecting Search
Ad

Similar to Java collections (20)

PPT
Collection Framework.power point presentation.......
PDF
javacollections.pdf
PPT
20CS305 Advance Java Programming Unit 1.ppt
PPT
List in java
PPTX
DOCX
Collections framework
DOCX
Collection frame work
PPT
Lists
PPTX
Nature Activities Binder _ by Slidesgo.pptx
PDF
Collections and generics
PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
PPTX
oop lecture framework,list,maps,collection
PPTX
LJ_JAVA_FS_Collection.pptx
PDF
java unit 4 pdf - about java collections
DOCX
ArrayList.docx
PPTX
Collection Framework-1.pptx
PDF
Collections in Java Notes
PDF
For this lab you will complete the class MyArrayList by implementing.pdf
PPT
12_-_Collections_Framework
Collection Framework.power point presentation.......
javacollections.pdf
20CS305 Advance Java Programming Unit 1.ppt
List in java
Collections framework
Collection frame work
Lists
Nature Activities Binder _ by Slidesgo.pptx
Collections and generics
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
oop lecture framework,list,maps,collection
LJ_JAVA_FS_Collection.pptx
java unit 4 pdf - about java collections
ArrayList.docx
Collection Framework-1.pptx
Collections in Java Notes
For this lab you will complete the class MyArrayList by implementing.pdf
12_-_Collections_Framework

Recently uploaded (20)

PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PPTX
IB Computer Science - Internal Assessment.pptx
PDF
Introduction to the R Programming Language
PDF
Mega Projects Data Mega Projects Data
PDF
annual-report-2024-2025 original latest.
PPTX
Computer network topology notes for revision
PPT
Quality review (1)_presentation of this 21
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
PDF
Fluorescence-microscope_Botany_detailed content
PPTX
Introduction to machine learning and Linear Models
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
Business Analytics and business intelligence.pdf
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Qualitative Qantitative and Mixed Methods.pptx
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
IB Computer Science - Internal Assessment.pptx
Introduction to the R Programming Language
Mega Projects Data Mega Projects Data
annual-report-2024-2025 original latest.
Computer network topology notes for revision
Quality review (1)_presentation of this 21
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
Fluorescence-microscope_Botany_detailed content
Introduction to machine learning and Linear Models
climate analysis of Dhaka ,Banglades.pptx
Introduction-to-Cloud-ComputingFinal.pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Business Analytics and business intelligence.pdf
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx

Java collections

  • 2. The List Interface  Method: void add(int index, Object o)  Inserts specified element at specified position Example: list.add(4, a); //adds element a at position 4
  • 3. The List Interface  Method: boolean add(Object o)  Adds specified element to the end of the list Example: list.add(a); //adds element a at the end of the list
  • 4. The List Interface  Method: boolean contains(Object 0)  Returns true if the specified element is in the list Example: list.add(a); list.contains(a); //returns true //////////////////////////////////////////////////////////// list.add(b); list.contains(a); //returns false
  • 5. The List Interface  Method: Object get(int index)  Returns the element at the specified position Example: list.add(a); list.add(b); list.get(1); //returns b
  • 6. The List Interface  Method: int indexOf(object o);  Returns the index of the specified element or else -1 Example: list.add(a); list.add(b); list.add(c); list.indexOf(c); //returns 2
  • 7. The List Interface  Method: int lastIndexOf(object o);  Returns the last index of the specified element or else -1 Example: list.add(a); list.add(b); list.add(a); list.indexOf(a); //returns 2
  • 8. The List Interface  Method: Object remove(int index)  Removes the element at the specified index Example: list.add(a); list.add(b); list.add(c); list.remove(1); //removes element a
  • 9. The List Interface  Method: boolean remove(Object o)  Removes the first occurrence of the specified element Example: list.add(a); list.add(b); list.add(a); list.remove(a); //removes element a at position 0
  • 10. The List Interface  Method: object set(int index, Object element)  Replaces the element at the specified position and returns the old element Example: list.add(a); list.set(0, b); //replaces element a with b and returns element a
  • 11. The List Interface  Method: boolean isEmpty()  Returns true if the method is empty Example list.isEmpty(); //retruns true //////////////////////////////////////////////////////////// list.add(a); list.isEmpty(); //returns false
  • 12. The List Interface  Method: int size()  Returns the number of elements in the list Example: list.add(a); list.add(b); list.add(c); list.size(); //Returns 3
  • 13. The List Interface  Method: boolean addAll(Collection c)  Appends all the elements in the specified collection to the end of the list Example: list2.add(a); list2.add(b); list2.add(c); list.addAll(list); //adds all elements in list2 to list
  • 14. The List Interface  Method: boolean addAll(int index, Collection c)  Appends all the elements in the specified collection to the specified position Example: list2.add(a); list2.add(b); list.add(a); list.addAll(0,list); //adds all elements in list2 to list before element a
  • 15. The List Interface  Method: void clear()  Removes all elements from the list Example: list.add(a); list.add(b); list.clear(); //removes elements a and b
  • 16. The List Interface  Methods: boolean conatainsAll(Collection c)  Returns true if the list contains all the element in the collection Example: list2.add(a); list2.add(b); list.add(a); list.add(b); list2.containAll(list); //Returns true
  • 17. The List Interface  Method: equals(Object o)  Compares the specific object with the list for equality  Example list.add(a); list2.add(a); list.equals(list2); //Returns true /////////////////////////////////////////////////////////////////////////////// list.add(a); list2.add(b); list.equals(list2); //Returns false
  • 18. The List Interface  Method: Iterator iterator()  Returns an iterator over the elements in the list in proper sequence  Example Iterator iter = list.iterator(); //Creates an iterator for list
  • 19. The List Interface  Method: ListIterator listIterator()  Returns a list iterator over the elements in the list in proper sequence Example ListIterator iter = list.listIterator(); //Creates a list iterator
  • 20. The List Interface  Method: ListIterator listIterator(int index)  Returns a list iterator over the elements in the list in proper sequence, starting at the indicated position Example ListIterator iter = list.listIterator(2); //Creates a list iterator starting at 2
  • 21. The List Interface  Method: boolean removeAll(collection c)  Removes all of the elements specified from the list Example list2.add(a); list2.add(b); list2.add(c); list.add(a); list.add(b); list2.removeAll(list); //Removes elements a and b
  • 22. The List Interface  Method: boolean retainAll(collection c)  Retains only the elements specified in the list Example list2.add(a); list2.add(b); list2.add(c); list.add(a); list2.retainAll(list); //Removes elements b and c and keeps a
  • 23. The List Interface  Method: List subList(int fromIndex, int toIndex)  Returns a view of the portion of the list between the specified indexes Example list.add(a); list.add(b); list.add(c); list.add(d); list.subList(1,3); //returns a view of elements b,c,d
  • 24. The List Interface  Method: Object[] toArray()  Returns an array containg all of the elements in the list Example: list.add(1); list.add(2); list.add(3); int[] array = list.toArray(); //Creates an array conating all of lists elements: 1, 2, and 3
  • 25. The List Interface  Method: Object[] toArray(Object a)  Returns an array containg all of the elements in the list, witht the runtime/ size of the array according to the object Example: list.add(1); list.add(2); list.add(3); int[] array = list.toArray(list); //Creats an array conating all of lists elements: 1, 2, and 3 with size of list
  • 26. Iterator Interface  Method: boolean hasNext()  Returns true if there are any items in the next position Example: While(iter.hasNext()) //Checks if there is a next position iter.next();
  • 27. Iterator Interface  Method: Object next()  Returns the next item and moves to the next position if hasNext is true Example: While(iter.hasNext()) iter.next(); //moves the position of the array to the next spot
  • 28. Iterator Interface  Method: void remove()  Removes item returned by the most recent call of next Example: While(iter.hasNext()) iter.next(); iter.remove(); //clears the list
  • 29. Iterator Interface  Method: boolean hasPrevious()  Returns true if there are any items in the previous position Example: While(iter.hasPrevious()) //Checks if there is a previous position iter.previous();
  • 30. Iterator Interface  Method: Object previous()  Returns the previous item and moves to the position before the current location if hasPrevious is true Example: While(iter.hasPrevious()) iter.previous(); //moves the position of the array to the previous spot
  • 31. Iterator Interface  Method: Object nextIndex()  Returns the value of the next index or -1 if none Example: list.add(a); list.add(b); ListIterator iter = list.listIterator(); iter.nextIndex(); //returns b ///////////////////////////////////////////////////////////////// list.add(a); ListIterator iter = list.listIterator(); iter.nextIndex(); //returns -1
  • 32. Iterator Interface  Method: Object previousIndex()  Returns the value of the previous index or -1 if none Example: list.add(a); ListIterator iter = list.listIterator(); iter.previousIndex(); //returns -1 ///////////////////////////////////////////////////////////////// list.add(a); list.add(b); iter.next(); ListIterator iter = list.listIterator(); iter.previousIndex(); //returns a
  • 33. Iterator Interface  Method: void add(Object 0)  Inserts an object o at the current pposition Example: list.add(a); list.add(b); ListIterator iter = list.listIterator(); iter.add(z); // list looks like (z,a,b)
  • 34. Iterator Interface  Method: void set(Object o)  Replaces the last item returned by next or previous Example: list.add(a); list.add(b); ListIterator iter = list.listIterator(); iter.next(); iter.set(z); //List looks like (a,z)