SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (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
 

Andere mochten auch

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 workingrockingprashik
 
Functional block diagram_of_laser_printer
Functional block diagram_of_laser_printerFunctional block diagram_of_laser_printer
Functional block diagram_of_laser_printerKartik Kalpande Patil
 

Andere mochten auch (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
 

Ähnlich wie Presentation1

Collections Framework
Collections FrameworkCollections Framework
Collections FrameworkSunil OS
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaEdureka!
 
Collections generic
Collections genericCollections generic
Collections genericsandhish
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & setsRatnaJava
 
Collections in .net technology (2160711)
Collections in .net technology (2160711)Collections in .net technology (2160711)
Collections in .net technology (2160711)Janki Shah
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work pptRanjith Alappadan
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionssuseredfbe9
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & CollectionArya
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & CollectionArya
 
collectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptxcollectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptxhemanth248901
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_FrameworkKrishna Sujeer
 
Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40bhawna sharma
 

Ähnlich wie 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
 

Mehr von Anand Grewal

Mehr von 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
 

Kürzlich hochgeladen

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
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 POSCeline George
 
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)Jisc
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
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).pptxVishalSingh1417
 
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).pptxEsquimalt MFRC
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
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...pradhanghanshyam7136
 
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 17Celine George
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
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_...Pooja Bhuva
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
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_.pdfSherif Taha
 
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.pptxMaritesTamaniVerdade
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
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 17Celine George
 
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...Poonam Aher Patil
 
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.pptxheathfieldcps1
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
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 FellowsMebane Rash
 

Kürzlich hochgeladen (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
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
 
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)
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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
 
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
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
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...
 
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
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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_...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
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
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
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
 
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...
 
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
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
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
 

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