SlideShare una empresa de Scribd logo
1 de 23
The Collection Framework
Collections Framework


 A collection is a group of objects.
 The Collection Framework Standardizes the way in
 which group of objects are handled by your program.

What c++ calls a Container,Java calls a Collection
The Collection interfaces


 Collection- enable you to work with groups of
 objects.

 List- extends collection to handle sequences.


 Set- extends collection to handle sets, which must
 contain unique elements.
The Collection Classes


 ArrayList – implements a dynamic array.
 LinkedList – implements a linked list.
 HashSet – uses hash table.
 linkedHashSet – allow insertion-order iterations.
 TreeSet – implements a set stored in a tree.
ArrayList class


 ArrayList class implements the List interface.
 ArrayList supports dynamic arrays that can grow as needed.
 It is a Variable length array of object references.


 ArrayLists are created with an initial size. When this size is
  enlarged, the collection is automatically enlarged. And when the
  objects are removed, the array may be shrunk.
Using constructor


 ArrayList()
 ArrayList(Collection c)
 ArrayList(int capacity)
Methods and Examples

 Create an array list
       ArrayList a1 = new ArrayList();

 Add elements to the array list
      a1.add(“c”);
      a1.add(1,“A2”);
 Display the array list
    System.out.println(a1);

 Remove elements from the array list
    a1.remove(“F”);
    a1.remove(2);

 For finding the size of arrayList
      a1.size();
Obtaining an Array from an ArrayList


 Add elements to the array list
   a1.add(new Integer(1));

 Get array
    object ia[ ] = a1.toArray();
LinkedList Class


 LinkedList class extends AbstractSequentialList and
 implements the List interface.

 It provides a linked list data structure.
Constructor


 LinkedList()


 LinkedList(collection c)
Methods

 To add elements at the first /last position.
        addFirst();
        addLast();

 To retrieve the elements at first /last position.
        getFirst();
        getLast();

 To remove the elements at first /last position.
        removeFirst();
        removeLast();
TreeSet class


 TreeSet provides an implementations of the Set
 interface that uses a tree for storage.

 Objects are stored in sorted order i.e ascending
 order.

 Access and retrieval times are quite fast.
Constructors

 TreeSet()
 TreeSet(collection c)
 TreeSet(comparator comp)
 TreeSet(SortedSet ss)
Example

Import java.util.*;
Class TreeSetDemo
{
Public static void main(String args[])
{
TreeSet ts=new TreeSet();
ts.add(“C”);
ts.add(“A”);
ts.add(“B”);
System.out.println(ts);
}
}

Output-    [A,B,C]
HashSet class

 HashSet extends AbstractSet and implements the Set
  interface.
 It creates a collection that uses a hash table for storage.
 Basic operations:
                        add()
                        contains()
                        remove()
                        size()
Using constructor

 HashSet()
 HashSet(collection c)
 HashSet(int capacity)
 HashSet(int capacity, float fillRatio)
Methods

 boolean add(Object o)
 void clear()
 Object clone()
 boolean contains(Object o)
 boolean isEmpty()
 Iterator iterator()
 boolean remove(Object o)
 int size()
Example

import java.util.*;
class hashset
{
public static void main(String args[])
{
HashSet hs = new HashSet();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
hs.remove("C");
System.out.println("elements after removing C"+hs);
 }
 }
LinkedHashSet class


 LinkedHashSet extends HashSet, but adds no
  members of its own.
 LinkedHashSet maintains a linked list of the entries
  in the set.
Using constructor


 LinkedHashSet( )
 LinkedHashSet(Collection c)
 LinkedHashSet(int capacity)
 LinkedHashSet(int capacity, float fillRatio)
Remove all elements from LinkedHashSet

import java.util.LinkedHashSet;
 public class linkedhashset
 {
   public static void main(String[] args)
     {
      LinkedHashSet lhashSet = new LinkedHashSet();
         lhashSet.add(new Integer("1"));
         lhashSet.add(new Integer("2"));
         lhashSet.add(new Integer("3"));
     System.out.println("LinkedHashSet before removal : " + lhashSet);
         lhashSet.clear();
     System.out.println("LinkedHashSet after removal : " + lhashSet);
     System.out.println("Is LinkedHashSet empty ? " + lhashSet.isEmpty());
   }
}
House Is Open For Queries

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
07 java collection
07 java collection07 java collection
07 java collection
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
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
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
List in java
List in javaList in java
List in java
 
Java collection
Java collectionJava collection
Java collection
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Linked list
Linked listLinked list
Linked list
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 

Destacado (8)

Detailed description about Hubs Switches Modems and their working
Detailed description about  Hubs Switches Modems and their workingDetailed description about  Hubs Switches Modems and their working
Detailed description about Hubs Switches Modems and their working
 
Functional block diagram_of_laser_printer
Functional block diagram_of_laser_printerFunctional block diagram_of_laser_printer
Functional block diagram_of_laser_printer
 
Pptemail
PptemailPptemail
Pptemail
 
Modem
ModemModem
Modem
 
Introduction to modem
Introduction to modemIntroduction to modem
Introduction to modem
 
Modem
ModemModem
Modem
 
Presentation on modem
Presentation on modemPresentation on modem
Presentation on modem
 
Modem presentation
Modem presentationModem presentation
Modem presentation
 

Similar a Presentation1

oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
ssuseredfbe9
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & Collection
Arya
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & Collection
Arya
 
collectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptxcollectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptx
hemanth248901
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_Framework
Krishna Sujeer
 

Similar a Presentation1 (20)

Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 
ArrayList.docx
ArrayList.docxArrayList.docx
ArrayList.docx
 
20 ch22 collections
20 ch22 collections20 ch22 collections
20 ch22 collections
 
Collections
CollectionsCollections
Collections
 
Array list(1)
Array list(1)Array list(1)
Array list(1)
 
Collections generic
Collections genericCollections generic
Collections generic
 
16 containers
16   containers16   containers
16 containers
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & sets
 
Collection Framework-1.pptx
Collection Framework-1.pptxCollection Framework-1.pptx
Collection Framework-1.pptx
 
Collections in .net technology (2160711)
Collections in .net technology (2160711)Collections in .net technology (2160711)
Collections in .net technology (2160711)
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work ppt
 
Java.util
Java.utilJava.util
Java.util
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & Collection
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & Collection
 
collectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptxcollectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptx
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_Framework
 
Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40
 

Más de Anand Grewal

Más de Anand Grewal (12)

distributed dbms
distributed dbmsdistributed dbms
distributed dbms
 
Transaction concurrency control
Transaction concurrency controlTransaction concurrency control
Transaction concurrency control
 
Object modeling
Object modelingObject modeling
Object modeling
 
Object analysis and design
Object analysis and designObject analysis and design
Object analysis and design
 
Object modeling
Object modelingObject modeling
Object modeling
 
O ops concepts
O ops conceptsO ops concepts
O ops concepts
 
System design
System designSystem design
System design
 
Presentation12
Presentation12Presentation12
Presentation12
 
Event handling
Event handlingEvent handling
Event handling
 
Isp
IspIsp
Isp
 
Java
JavaJava
Java
 
Presentation on dns
Presentation on dnsPresentation on dns
Presentation on dns
 

Último

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
 

Último (20)

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
 
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
 
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.
 
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
 
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
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
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
 
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
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
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...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
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
 
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
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
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 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
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

Presentation1

  • 2. Collections Framework  A collection is a group of objects.  The Collection Framework Standardizes the way in which group of objects are handled by your program. What c++ calls a Container,Java calls a Collection
  • 3. The Collection interfaces  Collection- enable you to work with groups of objects.  List- extends collection to handle sequences.  Set- extends collection to handle sets, which must contain unique elements.
  • 4. The Collection Classes  ArrayList – implements a dynamic array.  LinkedList – implements a linked list.  HashSet – uses hash table.  linkedHashSet – allow insertion-order iterations.  TreeSet – implements a set stored in a tree.
  • 5. ArrayList class  ArrayList class implements the List interface.  ArrayList supports dynamic arrays that can grow as needed.  It is a Variable length array of object references.  ArrayLists are created with an initial size. When this size is enlarged, the collection is automatically enlarged. And when the objects are removed, the array may be shrunk.
  • 6. Using constructor  ArrayList()  ArrayList(Collection c)  ArrayList(int capacity)
  • 7. Methods and Examples  Create an array list ArrayList a1 = new ArrayList();  Add elements to the array list a1.add(“c”); a1.add(1,“A2”);
  • 8.  Display the array list System.out.println(a1);  Remove elements from the array list a1.remove(“F”); a1.remove(2);  For finding the size of arrayList a1.size();
  • 9. Obtaining an Array from an ArrayList  Add elements to the array list a1.add(new Integer(1));  Get array object ia[ ] = a1.toArray();
  • 10. LinkedList Class  LinkedList class extends AbstractSequentialList and implements the List interface.  It provides a linked list data structure.
  • 12. Methods  To add elements at the first /last position. addFirst(); addLast();  To retrieve the elements at first /last position. getFirst(); getLast();  To remove the elements at first /last position. removeFirst(); removeLast();
  • 13. TreeSet class  TreeSet provides an implementations of the Set interface that uses a tree for storage.  Objects are stored in sorted order i.e ascending order.  Access and retrieval times are quite fast.
  • 14. Constructors  TreeSet()  TreeSet(collection c)  TreeSet(comparator comp)  TreeSet(SortedSet ss)
  • 15. Example Import java.util.*; Class TreeSetDemo { Public static void main(String args[]) { TreeSet ts=new TreeSet(); ts.add(“C”); ts.add(“A”); ts.add(“B”); System.out.println(ts); } } Output- [A,B,C]
  • 16. HashSet class  HashSet extends AbstractSet and implements the Set interface.  It creates a collection that uses a hash table for storage.  Basic operations: add() contains() remove() size()
  • 17. Using constructor  HashSet()  HashSet(collection c)  HashSet(int capacity)  HashSet(int capacity, float fillRatio)
  • 18. Methods  boolean add(Object o)  void clear()  Object clone()  boolean contains(Object o)  boolean isEmpty()  Iterator iterator()  boolean remove(Object o)  int size()
  • 19. Example import java.util.*; class hashset { public static void main(String args[]) { HashSet hs = new HashSet(); hs.add("B"); hs.add("A"); hs.add("D"); hs.add("E"); hs.add("C"); hs.add("F"); System.out.println(hs); hs.remove("C"); System.out.println("elements after removing C"+hs); } }
  • 20. LinkedHashSet class  LinkedHashSet extends HashSet, but adds no members of its own.  LinkedHashSet maintains a linked list of the entries in the set.
  • 21. Using constructor  LinkedHashSet( )  LinkedHashSet(Collection c)  LinkedHashSet(int capacity)  LinkedHashSet(int capacity, float fillRatio)
  • 22. Remove all elements from LinkedHashSet import java.util.LinkedHashSet; public class linkedhashset { public static void main(String[] args) { LinkedHashSet lhashSet = new LinkedHashSet(); lhashSet.add(new Integer("1")); lhashSet.add(new Integer("2")); lhashSet.add(new Integer("3")); System.out.println("LinkedHashSet before removal : " + lhashSet); lhashSet.clear(); System.out.println("LinkedHashSet after removal : " + lhashSet); System.out.println("Is LinkedHashSet empty ? " + lhashSet.isEmpty()); } }
  • 23. House Is Open For Queries