13 collections framework

18
13 Collections Framework

Upload: lucien

Post on 09-Jan-2016

28 views

Category:

Documents


0 download

DESCRIPTION

13 Collections Framework. Contents. What is Collection? Collections Framework Collections Hierarchy Collections Implementations Set List Map. Objectives. Define a collection Describe the collections framework Describe the collections hierarchy - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 13 Collections Framework

13 Collections Framework

Page 2: 13 Collections Framework

2

ContentsContents

• What is Collection?• Collections Framework• Collections Hierarchy• Collections Implementations

• Set

• List

• Map

Page 3: 13 Collections Framework

3

ObjectivesObjectives

• Define a collection• Describe the collections framework• Describe the collections hierarchy• Demonstrate each collection implementation

Page 4: 13 Collections Framework

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.

Page 5: 13 Collections Framework

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

Page 6: 13 Collections Framework

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

Page 7: 13 Collections Framework

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”

Page 8: 13 Collections Framework

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

Page 9: 13 Collections Framework

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

Page 10: 13 Collections Framework

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();

Page 11: 13 Collections Framework

11

Collections HierarchySet and List

Collections HierarchySet and List

HashSet

Collection

SortedSet

ListSet

LinkedHashSet TreeSet LinkedList Vector ArrayList

implements

implements

implements

implements extends

extends

Page 12: 13 Collections Framework

12

Collections HierarchyMap

Collections HierarchyMap

Map

SortedMap

Hashtable

LinkedHashMap

HashMap TreeMap

implements

implementsextends

extends

Page 13: 13 Collections Framework

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!

Page 14: 13 Collections Framework

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

Page 15: 13 Collections Framework

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

Page 16: 13 Collections Framework

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

Page 17: 13 Collections Framework

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

Page 18: 13 Collections Framework

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.