SlideShare una empresa de Scribd logo
1 de 21
JAVA
Collection classes
Prepared by
Miss. Arati A. Gadgil
Collections
•Collection represents a single unit of objects i.e. a group
•java.util package contains all the classes and interfaces for
Collection framework.
2
3
Methods
public boolean add(Object element)
is used to insert an element in this collection.
public boolean addAll(collection c)
is used to insert the specified collection elements in the
invoking collection.
public boolean remove(Object element)
is used to delete an element from this collection.
public boolean removeAll(Collection c)
is used to delete all the elements of specified collection
from the invoking collection.
4
public boolean retainAll(Collection c)
is used to delete all the elements of invoking collection except
the specified collection
public int size()
return the total number of elements in the collection.
public void clear()
removes the total no of element from the collection.
public boolean contains(object element)
is used to search an element.
public boolean containsAll(Collection c)
is used to search the specified collection in this collection 5
public Iterator iterator()
returns an iterator.
public Object[] toArray()
converts collection into array.
public boolean isEmpty()
checks if collection is empty.
public boolean equals(Object element)
matches two collection
public int hashCode()
returns the hashcode number for collection.
6
7
Iterator interface
Iterator interface provides the facility of iterating the elements
in forward direction only.
Methods
1.public boolean hasNext()
it returns true if iterator has more elements.
2.public object next()
it returns the element and moves the cursor pointer to the
next element.
3.public void remove()
it removes the last elements returned by the iterator. It is
rarely used.
Java ArrayList class
Java ArrayList class uses a dynamic array for storing the elements.It
extends AbstractList class and implements List interface.
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 be occurred if any element is removed from the array list.
8
Java LinkedList class
Java LinkedList class uses doubly linked list to store the elements. It
extends the AbstractList class and implements List and Deque interfaces.
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 be occurred.
Java LinkedList class can be used as list, stack or queue
9
Difference between ArrayList and LinkedList
ArrayList LinkedList
1) ArrayList internally uses dynamic
array to store the elements.
LinkedList internally uses doubly linked
list to store the elements.
2) Manipulation with ArrayList
is slow because it internally uses array. If
any element is removed from the array,
all the bits are shifted in memory.
Manipulation with LinkedList
is faster than ArrayList because it uses
doubly linked list so no bit shifting is
required in memory.
3) ArrayList class can act as a list only
because it implements List only.
LinkedList class can act as a list and
queue both because it implements List
and Deque interfaces.
4) ArrayList is better for storing and
accessing data.
LinkedList is better for
manipulating data.
10
Java List Interface
List Interface is the subinterface of Collection.It contains methods to
insert and delete elements in index basis.It is a factory of ListIterator
interface.
Commonly used methods of List Interface:
public void add(int index,Object element);
public boolean addAll(int index,Collection c);
public object get(int Index position);
public object set(int index,Object element);
public object remove(int index);
public ListIterator listIterator();
public ListIterator listIterator(int i);
11
Java ListIterator Interface
ListIterator Interface is used to traverse the element in backward and
forward direction.
Commonly used methods of ListIterator Interface:
public boolean hasNext();
public Object next();
public boolean hasPrevious();
public Object previous();
12
Difference between List and Set
List can contain duplicate elements whereas Set contains unique
elements only.
Java HashSet class
uses hash table to store the elements. It extends AbstractSet class and
implements Set interface.
contains unique elements only
13
Java Queue Interface
Methods of Queue Interface
public boolean add(object);
public boolean offer(object);
public remove();
public poll();
public element();
public peek();
14
Java Map Interface
A map contains values based on the key i.e. key and value pair.Each
pair is known as an entry.Map contains only unique elements.
Commonly used methods of Map interface:
public Object put(object key,Object value):
is used to insert an entry in this map.
public void putAll(Map map)
is used to insert the specified map in this map.
public Object remove(object key)
is used to delete an entry for the specified key.
15
public Object get(Object key)
is used to return the value for the specified key.
public boolean containsKey(Object key)
is used to search the specified key from this map.
public boolean containsValue(Object value)
is used to search the specified value from this map.
public Set keySet()
returns the Set view containing all the keys.
public Set entrySet()
returns the Set view containing all the keys and values.
16
Entry
Entry is the subinterface of Map.So we will access it by Map.Entry
name.It provides methods to get key and value.
Methods of Entry interface:
public Object getKey()
is used to obtain key.
public Object getValue()
is used to obtain value
17
Sorting
We can sort the elements of:
String objects
Wrapper class objects
User-defined class objects
Method
public void sort(List list)
is used to sort the elements of List.List elements must be of
Comparable type.
18
19
Java Collections Framework
The Java Collections Framework is a collection of interfaces and classes
which helps in storing and processing the data efficiently. This
framework has several useful classes which have tons of useful functions
which makes a programmer task super easy.
20
Thank You
21

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Java Collections
Java  Collections Java  Collections
Java Collections
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
L11 array list
L11 array listL11 array list
L11 array list
 
Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
Java collections
Java collectionsJava collections
Java collections
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in 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)
 
Java exception
Java exception Java exception
Java exception
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work ppt
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Java 8 Streams
Java 8 StreamsJava 8 Streams
Java 8 Streams
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
sets and maps
 sets and maps sets and maps
sets and maps
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 

Similar a Java collection

oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
ssuseredfbe9
 

Similar a Java collection (20)

Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshan
 
Advanced core java
Advanced core javaAdvanced core java
Advanced core java
 
Collection Framework-1.pptx
Collection Framework-1.pptxCollection Framework-1.pptx
Collection Framework-1.pptx
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptx
 
22.collections(1)
22.collections(1)22.collections(1)
22.collections(1)
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
ArrayList.docx
ArrayList.docxArrayList.docx
ArrayList.docx
 
Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
List interface in collections framework
List interface in collections frameworkList interface in collections framework
List interface in collections framework
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
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 )
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
 
Java.util
Java.utilJava.util
Java.util
 
Collections framework
Collections frameworkCollections framework
Collections framework
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
Collections
CollectionsCollections
Collections
 
20 ch22 collections
20 ch22 collections20 ch22 collections
20 ch22 collections
 
Lecture 24
Lecture 24Lecture 24
Lecture 24
 

Más de Arati Gadgil

Más de Arati Gadgil (15)

Java adapter
Java adapterJava adapter
Java adapter
 
Java swing
Java swingJava swing
Java swing
 
Java applet
Java appletJava applet
Java applet
 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
 
Java awt
Java awtJava awt
Java awt
 
Java stream
Java streamJava stream
Java stream
 
Java thread
Java threadJava thread
Java thread
 
Java networking
Java networkingJava networking
Java networking
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Java package
Java packageJava package
Java package
 
Java interface
Java interfaceJava interface
Java interface
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandling
 
Java class
Java classJava class
Java class
 
Java basic
Java basicJava basic
Java basic
 

Último

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Último (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 

Java collection

  • 2. Collections •Collection represents a single unit of objects i.e. a group •java.util package contains all the classes and interfaces for Collection framework. 2
  • 3. 3
  • 4. Methods public boolean add(Object element) is used to insert an element in this collection. public boolean addAll(collection c) is used to insert the specified collection elements in the invoking collection. public boolean remove(Object element) is used to delete an element from this collection. public boolean removeAll(Collection c) is used to delete all the elements of specified collection from the invoking collection. 4
  • 5. public boolean retainAll(Collection c) is used to delete all the elements of invoking collection except the specified collection public int size() return the total number of elements in the collection. public void clear() removes the total no of element from the collection. public boolean contains(object element) is used to search an element. public boolean containsAll(Collection c) is used to search the specified collection in this collection 5
  • 6. public Iterator iterator() returns an iterator. public Object[] toArray() converts collection into array. public boolean isEmpty() checks if collection is empty. public boolean equals(Object element) matches two collection public int hashCode() returns the hashcode number for collection. 6
  • 7. 7 Iterator interface Iterator interface provides the facility of iterating the elements in forward direction only. Methods 1.public boolean hasNext() it returns true if iterator has more elements. 2.public object next() it returns the element and moves the cursor pointer to the next element. 3.public void remove() it removes the last elements returned by the iterator. It is rarely used.
  • 8. Java ArrayList class Java ArrayList class uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface. 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 be occurred if any element is removed from the array list. 8
  • 9. Java LinkedList class Java LinkedList class uses doubly linked list to store the elements. It extends the AbstractList class and implements List and Deque interfaces. 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 be occurred. Java LinkedList class can be used as list, stack or queue 9
  • 10. Difference between ArrayList and LinkedList ArrayList LinkedList 1) ArrayList internally uses dynamic array to store the elements. LinkedList internally uses doubly linked list to store the elements. 2) Manipulation with ArrayList is slow because it internally uses array. If any element is removed from the array, all the bits are shifted in memory. Manipulation with LinkedList is faster than ArrayList because it uses doubly linked list so no bit shifting is required in memory. 3) ArrayList class can act as a list only because it implements List only. LinkedList class can act as a list and queue both because it implements List and Deque interfaces. 4) ArrayList is better for storing and accessing data. LinkedList is better for manipulating data. 10
  • 11. Java List Interface List Interface is the subinterface of Collection.It contains methods to insert and delete elements in index basis.It is a factory of ListIterator interface. Commonly used methods of List Interface: public void add(int index,Object element); public boolean addAll(int index,Collection c); public object get(int Index position); public object set(int index,Object element); public object remove(int index); public ListIterator listIterator(); public ListIterator listIterator(int i); 11
  • 12. Java ListIterator Interface ListIterator Interface is used to traverse the element in backward and forward direction. Commonly used methods of ListIterator Interface: public boolean hasNext(); public Object next(); public boolean hasPrevious(); public Object previous(); 12
  • 13. Difference between List and Set List can contain duplicate elements whereas Set contains unique elements only. Java HashSet class uses hash table to store the elements. It extends AbstractSet class and implements Set interface. contains unique elements only 13
  • 14. Java Queue Interface Methods of Queue Interface public boolean add(object); public boolean offer(object); public remove(); public poll(); public element(); public peek(); 14
  • 15. Java Map Interface A map contains values based on the key i.e. key and value pair.Each pair is known as an entry.Map contains only unique elements. Commonly used methods of Map interface: public Object put(object key,Object value): is used to insert an entry in this map. public void putAll(Map map) is used to insert the specified map in this map. public Object remove(object key) is used to delete an entry for the specified key. 15
  • 16. public Object get(Object key) is used to return the value for the specified key. public boolean containsKey(Object key) is used to search the specified key from this map. public boolean containsValue(Object value) is used to search the specified value from this map. public Set keySet() returns the Set view containing all the keys. public Set entrySet() returns the Set view containing all the keys and values. 16
  • 17. Entry Entry is the subinterface of Map.So we will access it by Map.Entry name.It provides methods to get key and value. Methods of Entry interface: public Object getKey() is used to obtain key. public Object getValue() is used to obtain value 17
  • 18. Sorting We can sort the elements of: String objects Wrapper class objects User-defined class objects Method public void sort(List list) is used to sort the elements of List.List elements must be of Comparable type. 18
  • 19. 19 Java Collections Framework The Java Collections Framework is a collection of interfaces and classes which helps in storing and processing the data efficiently. This framework has several useful classes which have tons of useful functions which makes a programmer task super easy.
  • 20. 20