SlideShare une entreprise Scribd logo
1  sur  22
Collections
A collection — sometimes
 called a container — is simply
 an object that groups multiple
 elements into a single unit.
 Collections are used to
 store, retrieve, manipulate, an
 d communicate aggregate
 data.
Characteristics
• All the core collection interfaces are generic.
     public interface Collection<E>...
• Ordered & Unordered
• Sorted & Unsorted
What Is a Collections Framework?

is a unified architecture for representing and manipulating collections.
    Contains:

• Interfaces: These are abstract data types that represent collections.
  Interfaces allow collections to be manipulated independently of the
  details of their representation. In object-oriented languages, interfaces
  generally form a hierarchy.

• Implementations: These are the concrete implementations of the
  collection interfaces. In essence, they are reusable data structures.

• Algorithms: These are the methods that perform useful
  computations, such as searching and sorting, on objects that
  implement collection interfaces. The algorithms are said to be
  polymorphic: that is, the same method can be used on many different
  implementations of the appropriate collection interface. In
  essence, algorithms are reusable functionality.
Benefits of the Collections Framework

• Reduces programming effort: By providing
  useful data structures and algorithms, the
  Collections Framework frees you to
  concentrate on the important parts of your
  program rather than on the low-level
  "plumbing" required to make it work. By
  facilitating interoperability among unrelated
  APIs, the Java Collections Framework frees
  you from writing adapter objects or
  conversion code to connect APIs.
Benefits of the Collections Framework

• Increases program speed and quality: This
  Collections Framework provides high-
  performance, high-quality implementations of
  useful data structures and algorithms. The
  various implementations of each interface are
  interchangeable, so programs can be easily tuned
  by switching collection implementations. Because
  you're freed from the drudgery of writing your
  own data structures, you'll have more time to
  devote to improving programs' quality and
  performance.
Benefits of the Collections Framework
• Allows interoperability among unrelated
  APIs: The collection interfaces are the
  vernacular by which APIs pass collections back
  and forth. If my network administration API
  furnishes a collection of node names and if
  your GUI toolkit expects a collection of
  column headings, our APIs will interoperate
  seamlessly, even though they were written
  independently.
Benefits of the Collections Framework
• Reduces effort to learn and to use new APIs:
  Many APIs naturally take collections on input and
  furnish them as output. In the past, each such API
  had a small sub-API devoted to manipulating its
  collections. There was little consistency among
  these ad hoc collections sub-APIs, so you had to
  learn each one from scratch, and it was easy to
  make mistakes when using them. With the
  advent of standard collection interfaces, the
  problem went away.
Benefits of the Collections Framework
• Reduces effort to design new APIs: This is the flip
  side of the previous advantage. Designers and
  implementers don't have to reinvent the wheel
  each time they create an API that relies on
  collections; instead, they can use standard
  collection interfaces.
• Fosters software reuse: New data structures that
  conform to the standard collection interfaces are
  by nature reusable. The same goes for new
  algorithms that operate on objects that
  implement these interfaces.
Main Interfaces
Collection
• The root of the collection hierarchy.
• Represents a group of objects known as its
  elements.
• The least common denominator that all
  collections implement and is used to pass
  collections around and to manipulate them
• Some types of collections allow duplicate
  elements, and others do not. Some are
  ordered and others are unordered.
Set
• Cannot contain duplicate elements
• Models the mathematical set abstraction and
  is used to represent sets, such as the courses
  making up a student's schedule
• SortedSet : a Set that maintains its elements
  in ascending order. Several additional
  operations are provided to take advantage of
  the ordering.
List
•   Ordered
•   Sometimes called a sequence
•   Can contain duplicate elements.
•   User of a List generally has precise control
    over where in the list each element is inserted
    and can access elements by their integer index
    (position)
Queue
• Used to hold multiple elements prior to
  processing.
• Besides basic Collection operations, a Queue
  provides additional insertion, extraction, and
  inspection operations.
• Queues typically, but do not necessarily, order
  elements in a FIFO manner
Map
•   Object that maps keys to values.
•    Cannot contain duplicate keys
•    each key can map to at most one value.
•   SortedMap : a Map that maintains its
    mappings in ascending key order. This is the
    Map analog of SortedSet. Sorted maps are
    used for naturally ordered collections of
    key/value pairs, such as dictionaries and
    telephone directories
Autoboxing
Collections can hold Objects but not primitives
• Prior to Java 5, you had to wrap a primitive by
  hand
  – myInts.add(new Integer(42));


• With Java 5, primitives still have to be
  wrapped, but autoboxing
  – myInts.add(42);
Sorting Collections
• ArrayList doesn't give you any way to sort its
  contents, but the java.util.Collections class
  does
  – ArrayList<String> stuff = new ArrayList<String>();
  – Collections.sort(stuff);


• What is going to happen if we used the same
  function with objects ?
Comparable Interface
• The Comparable interface is used by the
  Collections.sort() method and the
  java.util.Arrays.sort() method to sort Lists and
  arrays of objects
• To implement Comparable, a class must
  implement a single method, compareTo()
Comparable Interface
• The compareTo() method returns an int with
  the following characteristics:
  – negative If thisObject < anotherObject
  – zero If thisObject == anotherObject
  – positive If thisObject > anotherObject
class DVDInfo implements Comparable<DVDInfo> {
// existing code
   public int compareTo(DVDInfo d) {
     return title.compareTo(d.getTitle());
   }
 }
Sorting with Comparator
• The Comparator interface gives you the
  capability to sort a given collection any
  number of different ways
• you can use it to sort instances of any class—
  even classes you can't modify —unlike the
  Comparable interface, which forces you to
  change the class whose instances you want to
  sort.
• The Comparator.compare() method returns an
  int whose meaning is the same as the
  Comparable.compareTo() method's return
  value.
  import java.util.*;
  class GenreSort implements Comparator<DVDInfo> {
     public int compare(DVDInfo one, DVDInfo two) {
        return one.getGenre().compareTo(two.getGenre());
      }
  }

Contenu connexe

Tendances

Learn advanced java programming
Learn advanced java programmingLearn advanced java programming
Learn advanced java programmingTOPS Technologies
 
Collections in Java
Collections in JavaCollections in Java
Collections in JavaKhasim Cise
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work pptRanjith Alappadan
 
Advanced c++ ver 1.1
Advanced c++ ver 1.1Advanced c++ ver 1.1
Advanced c++ ver 1.1Vu Nguyen
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection frameworkankitgarg_er
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 
Collections Java e Google Collections
Collections Java e Google CollectionsCollections Java e Google Collections
Collections Java e Google CollectionsAndré Faria Gomes
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
5 collection framework
5 collection framework5 collection framework
5 collection frameworkMinal Maniar
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator OverloadingMichael Heron
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Java collections
Java collectionsJava collections
Java collectionspadmad2291
 
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...Edureka!
 
Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40bhawna sharma
 

Tendances (20)

Learn advanced java programming
Learn advanced java programmingLearn advanced java programming
Learn advanced java programming
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work ppt
 
Advanced c++ ver 1.1
Advanced c++ ver 1.1Advanced c++ ver 1.1
Advanced c++ ver 1.1
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Collections Java e Google Collections
Collections Java e Google CollectionsCollections Java e Google Collections
Collections Java e Google Collections
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
Best core & advanced java classes in mumbai
Best core & advanced java classes in mumbaiBest core & advanced java classes in mumbai
Best core & advanced java classes in mumbai
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
07 java collection
07 java collection07 java collection
07 java collection
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Java collections
Java collectionsJava collections
Java collections
 
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 lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40
 

Similaire à Collections

1. Java Collections Framework Introduction
1. Java Collections Framework Introduction1. Java Collections Framework Introduction
1. Java Collections Framework IntroductionBharat Savani
 
‘go-to’ general-purpose sequential collections - from Java To Scala
‘go-to’ general-purpose sequential collections -from Java To Scala‘go-to’ general-purpose sequential collections -from Java To Scala
‘go-to’ general-purpose sequential collections - from Java To ScalaPhilip Schwarz
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)SURBHI SAROHA
 
Collectn framework copy
Collectn framework   copyCollectn framework   copy
Collectn framework copycharan kumar
 
Collectn framework
Collectn frameworkCollectn framework
Collectn frameworkcharan kumar
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.pptYonas D. Ebren
 
Java Collection Interview Questions [Updated]
Java Collection Interview Questions [Updated]Java Collection Interview Questions [Updated]
Java Collection Interview Questions [Updated]SoniaMathias2
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsRanel Padon
 
Generic types and collections GUIs.pptx
Generic types and collections GUIs.pptxGeneric types and collections GUIs.pptx
Generic types and collections GUIs.pptxAvirup Pal
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxeyemitra1
 
Java Hands-On Workshop
Java Hands-On WorkshopJava Hands-On Workshop
Java Hands-On WorkshopArpit Poladia
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Hemlathadhevi Annadhurai
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm. Abdul salam
 

Similaire à Collections (20)

Collections
CollectionsCollections
Collections
 
1. Java Collections Framework Introduction
1. Java Collections Framework Introduction1. Java Collections Framework Introduction
1. Java Collections Framework Introduction
 
14. collections
14. collections14. collections
14. collections
 
‘go-to’ general-purpose sequential collections - from Java To Scala
‘go-to’ general-purpose sequential collections -from Java To Scala‘go-to’ general-purpose sequential collections -from Java To Scala
‘go-to’ general-purpose sequential collections - from Java To Scala
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)
 
Collectn framework copy
Collectn framework   copyCollectn framework   copy
Collectn framework copy
 
Collectn framework
Collectn frameworkCollectn framework
Collectn framework
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
Java Collection Interview Questions [Updated]
Java Collection Interview Questions [Updated]Java Collection Interview Questions [Updated]
Java Collection Interview Questions [Updated]
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
 
Generic types and collections GUIs.pptx
Generic types and collections GUIs.pptxGeneric types and collections GUIs.pptx
Generic types and collections GUIs.pptx
 
Advanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptxAdvanced Java - UNIT 3.pptx
Advanced Java - UNIT 3.pptx
 
Java Hands-On Workshop
Java Hands-On WorkshopJava Hands-On Workshop
Java Hands-On Workshop
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 
Collection
CollectionCollection
Collection
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 

Collections

  • 2. A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, an d communicate aggregate data.
  • 3. Characteristics • All the core collection interfaces are generic. public interface Collection<E>... • Ordered & Unordered • Sorted & Unsorted
  • 4. What Is a Collections Framework? is a unified architecture for representing and manipulating collections. Contains: • Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy. • Implementations: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures. • Algorithms: These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality.
  • 5. Benefits of the Collections Framework • Reduces programming effort: By providing useful data structures and algorithms, the Collections Framework frees you to concentrate on the important parts of your program rather than on the low-level "plumbing" required to make it work. By facilitating interoperability among unrelated APIs, the Java Collections Framework frees you from writing adapter objects or conversion code to connect APIs.
  • 6. Benefits of the Collections Framework • Increases program speed and quality: This Collections Framework provides high- performance, high-quality implementations of useful data structures and algorithms. The various implementations of each interface are interchangeable, so programs can be easily tuned by switching collection implementations. Because you're freed from the drudgery of writing your own data structures, you'll have more time to devote to improving programs' quality and performance.
  • 7. Benefits of the Collections Framework • Allows interoperability among unrelated APIs: The collection interfaces are the vernacular by which APIs pass collections back and forth. If my network administration API furnishes a collection of node names and if your GUI toolkit expects a collection of column headings, our APIs will interoperate seamlessly, even though they were written independently.
  • 8. Benefits of the Collections Framework • Reduces effort to learn and to use new APIs: Many APIs naturally take collections on input and furnish them as output. In the past, each such API had a small sub-API devoted to manipulating its collections. There was little consistency among these ad hoc collections sub-APIs, so you had to learn each one from scratch, and it was easy to make mistakes when using them. With the advent of standard collection interfaces, the problem went away.
  • 9. Benefits of the Collections Framework • Reduces effort to design new APIs: This is the flip side of the previous advantage. Designers and implementers don't have to reinvent the wheel each time they create an API that relies on collections; instead, they can use standard collection interfaces. • Fosters software reuse: New data structures that conform to the standard collection interfaces are by nature reusable. The same goes for new algorithms that operate on objects that implement these interfaces.
  • 11. Collection • The root of the collection hierarchy. • Represents a group of objects known as its elements. • The least common denominator that all collections implement and is used to pass collections around and to manipulate them • Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered.
  • 12. Set • Cannot contain duplicate elements • Models the mathematical set abstraction and is used to represent sets, such as the courses making up a student's schedule • SortedSet : a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering.
  • 13. List • Ordered • Sometimes called a sequence • Can contain duplicate elements. • User of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position)
  • 14. Queue • Used to hold multiple elements prior to processing. • Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations. • Queues typically, but do not necessarily, order elements in a FIFO manner
  • 15. Map • Object that maps keys to values. • Cannot contain duplicate keys • each key can map to at most one value. • SortedMap : a Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories
  • 16. Autoboxing Collections can hold Objects but not primitives • Prior to Java 5, you had to wrap a primitive by hand – myInts.add(new Integer(42)); • With Java 5, primitives still have to be wrapped, but autoboxing – myInts.add(42);
  • 17. Sorting Collections • ArrayList doesn't give you any way to sort its contents, but the java.util.Collections class does – ArrayList<String> stuff = new ArrayList<String>(); – Collections.sort(stuff); • What is going to happen if we used the same function with objects ?
  • 18. Comparable Interface • The Comparable interface is used by the Collections.sort() method and the java.util.Arrays.sort() method to sort Lists and arrays of objects • To implement Comparable, a class must implement a single method, compareTo()
  • 19. Comparable Interface • The compareTo() method returns an int with the following characteristics: – negative If thisObject < anotherObject – zero If thisObject == anotherObject – positive If thisObject > anotherObject
  • 20. class DVDInfo implements Comparable<DVDInfo> { // existing code public int compareTo(DVDInfo d) { return title.compareTo(d.getTitle()); } }
  • 21. Sorting with Comparator • The Comparator interface gives you the capability to sort a given collection any number of different ways • you can use it to sort instances of any class— even classes you can't modify —unlike the Comparable interface, which forces you to change the class whose instances you want to sort.
  • 22. • The Comparator.compare() method returns an int whose meaning is the same as the Comparable.compareTo() method's return value. import java.util.*; class GenreSort implements Comparator<DVDInfo> { public int compare(DVDInfo one, DVDInfo two) { return one.getGenre().compareTo(two.getGenre()); } }