13 collections framework. 2 contents what is collection? collections framework collections hierarchy...
TRANSCRIPT
13 Collections Framework
2
ContentsContents
• What is Collection?• Collections Framework• Collections Hierarchy• Collections Implementations
• Set
• List
• Map
3
ObjectivesObjectives
• Define a collection• Describe the collections framework• Describe the collections hierarchy• Demonstrate each collection implementation
4
What is a Collection?What is a Collection?
• A Collection (also known as container) is an object that contains a group of objects treated as a single unit.
• Any type of objects can be stored, retrieved and manipulated as elements of collections.
5
Collections FrameworkCollections Framework
• Collections Framework is a unified architecture for managing collections
Main Parts of Collections Framework1. Interfaces
• Core interfaces defining common functionality exhibited by collections
2. Implementations• Concrete classes of the core interfaces providing data
structures
3. Operations• Methods that perform various operations on collections
6
Collections FrameworkInterfaces
Collections FrameworkInterfaces
Core Interface Description
Collection specifies contract that all collections should implement
Set defines functionality for a set of unique elements
SortedSet defines functionality for a set where elements are sorted
List defines functionality for an ordered list of non- unique elements
Map defines functionality for mapping of unique keys to values
SortedMap defines functionality for a map where its keys are sorted
7
Collections FrameworkImplementations
Collections FrameworkImplementations
Set List Map
HashSet ArrayList HashMap
LinkedHashSet LinkedList LinkedHashMap
TreeSet Vector Hashtable
TreeMap
Note: Hashtable uses a lower-case “t”
8
Collections FrameworkOperations
Collections FrameworkOperations
Basic collection operations:
• Check if collection is empty• Check if an object exists in collection.• Retrieve an object from collection• Add object to collection• Remove object from collection• Iterate collection and inspect each object
• Each operation has a corresponding method implementation for each collection type
9
Collections CharacteristicsCollections Characteristics
• Ordered• Elements are stored and accessed in a specific order
• Sorted• Elements are stored and accessed in a sorted order
• Indexed• Elements can be accessed using an index
• Unique• Collection does not allow duplicates
10
IteratorIterator
• An iterator is an object used to mark a position in a collection of data and to move from item to item within the collection
Syntax:Iterator <variable> = <CollectionObject>.iterator();
11
Collections HierarchySet and List
Collections HierarchySet and List
HashSet
Collection
SortedSet
ListSet
LinkedHashSet TreeSet LinkedList Vector ArrayList
implements
implements
implements
implements extends
extends
12
Collections HierarchyMap
Collections HierarchyMap
Map
SortedMap
Hashtable
LinkedHashMap
HashMap TreeMap
implements
implementsextends
extends
13
Set : Unique things (classes that implement Set)
Map : Things with a unique ID (classes that implement Map)
List : Lists of things (classes that implement List)
Collection ImplementationsCollection Implementations
Next!
14
A List cares about the index.A List cares about the index.
ListList
“Paul”“Paul” “Mark”“Mark” “John”“John” “Paul”“Paul” “Luke”“Luke”value
index 0 1 2 3 4
LinkedListLinkedListVectorVectorArrayListArrayList
18
A Set cares about uniqueness, it doesn’t allow duplicates.A Set cares about uniqueness, it doesn’t allow duplicates.
SetSet
“Paul”“Paul”
“Mark”“Mark”
“John”“John” “Luke”“Luke”
“Fred”“Fred”“Peter”“Peter”
TreeSetTreeSetLinkedHashSetLinkedHashSetHashSetHashSet
22
A Map cares about unique identifiers.A Map cares about unique identifiers.
MapMap
“Paul”“Paul” “Mark”“Mark” “John”“John” “Paul”“Paul” “Luke”“Luke”
key
value
“Pl” “Ma” “Jn” “ul” “Le”
LinkedHashMapLinkedHashMap TreeMapTreeMapHashtableHashtableHashMapHashMap
27
Collection Classes SummaryCollection Classes Summary
NoBy indexXLinkedList
NoBy indexXVector
NoBy indexXArrayList
NoBy insertion order or last access order
XLinkedHashSet
By natural order or custom comparison rules
SortedXTreeSet
NoNoXHashSet
NoBy insertion order or last access order
XLinkedHashMap
By natural order or custom comparison rules
SortedXTreeMap
NoNoXHashtable
NoNoXHashMap
SortedOrderedListSetMapClass
28
Key PointsKey Points
• Collections Framework contains:
1. Interfaces
2. Implementations
3. Operations• A list cares about the index.• A set cares about uniqueness, it does not allow
duplicates.• A map cares about unique identifiers.