SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Java Collections
Java CollectionsJava Collections
Java Collections
parag
 

Tendances (20)

Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
L11 array list
L11 array listL11 array list
L11 array list
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
07 java collection
07 java collection07 java collection
07 java collection
 
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...
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Java collections
Java collectionsJava collections
Java collections
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
Serialization & De-serialization in Java
Serialization & De-serialization in JavaSerialization & De-serialization in Java
Serialization & De-serialization in Java
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 

Similaire à 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
 

Similaire à 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
 

Plus de Arati Gadgil

Plus de Arati Gadgil (16)

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 exception
Java exception Java exception
Java exception
 
Java class
Java classJava class
Java class
 
Java basic
Java basicJava basic
Java basic
 

Dernier

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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
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
 

Dernier (20)

Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.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
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
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
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
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
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

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