Upload
gyles-flowers
View
218
Download
0
Tags:
Embed Size (px)
Citation preview
CollectionsCollections
Data structures in JavaData structures in Java
OBJECTIVEOBJECTIVE
“ “ WHEN TO USE WHICH DATA WHEN TO USE WHICH DATA STRUCTURESTRUCTURE ””
Debug
Data StructuresData Structures
1. A data structure is an 1. A data structure is an arrangement of data in a arrangement of data in a computer’s memory.computer’s memory.
2. It includes list,stack,binary trees, 2. It includes list,stack,binary trees,
hash tables, etc.hash tables, etc.
3. Algorithms manipulate the data in 3. Algorithms manipulate the data in these structures in various ways these structures in various ways such as searching and sorting.such as searching and sorting.
What is a Collections What is a Collections framework?framework?
A A frameworkframework is an extensive set of is an extensive set of interfaces, abstract classes and interfaces, abstract classes and concrete classes together with concrete classes together with support toolssupport tools
Framework is provided in Framework is provided in java.util java.util packagepackage & comprises three parts: & comprises three parts:
1. Core interfaces1. Core interfaces
2. Set of implementations. 2. Set of implementations.
3. Utility methods3. Utility methods
Need of a framework????Need of a framework????
Pre Java SDK1.2, Java provided a handful of Pre Java SDK1.2, Java provided a handful of data structures:data structures:
Hash TableHash Table VectorVector StackStack
These were good and easy to use, but they were These were good and easy to use, but they were not organized into a more general framework.not organized into a more general framework.
Lacked interoperabilityLacked interoperability..
Features of Collection Features of Collection frameworkframework
Reduces programming effortReduces programming effort..
Increases performance.Increases performance.
Provides interoperability between Provides interoperability between unrelated APIs.unrelated APIs.
Faster software reuse.Faster software reuse.
Interfaces and Interfaces and implementationimplementation
Collection
List
Set
SortedSet
Map
SortedMap
LinkedList ArrayList
HashSet
TreeSet
HashMap
TreeMap
Extends
Implements
Interface
Class
InterfacesInterfaces
Collection Collection Set, List Set, List
A group of objects.A group of objects.
May or may not be ordered;May or may not be ordered;
May or may not contain duplicates.May or may not contain duplicates.
Interface continuedInterface continued Set Set
The familiar set abstraction. The familiar set abstraction. No duplicates; May or may not be orderedNo duplicates; May or may not be ordered..
List List Ordered collection, also known as a sequence. Ordered collection, also known as a sequence. Duplicates permitted; Allows positional accessDuplicates permitted; Allows positional access
Map Map A mapping from keys to values. A mapping from keys to values. Each key can map to at most one value Each key can map to at most one value
(function).(function).
Iterators Iterators
A collection provides an iterator which A collection provides an iterator which allows sequential access to the elements allows sequential access to the elements of a collection.of a collection.
Methods:Methods: has Next() – check if there are still elementshas Next() – check if there are still elements next() – return the next object and advancenext() – return the next object and advance remove() – remove the currently pointed remove() – remove the currently pointed
objectobject
List interfaceList interface An interface that extends the An interface that extends the
Collections interface.Collections interface. An ordered collection . An ordered collection .
Stores element by positionStores element by position Includes index based operation.Includes index based operation.
Allows duplicate elementsAllows duplicate elements..
Concrete List Concrete List ImplementationsImplementations
There are two concrete implementations There are two concrete implementations of the List interfaceof the List interface LinkedListLinkedList ArrayListArrayList
Which is best to use depends on specific Which is best to use depends on specific needs.needs.
Array ListArray List Stores element in a contiguous block Stores element in a contiguous block
of memory and automatically of memory and automatically expandable.expandable.
The collection efficiently (O(1)) The collection efficiently (O(1)) inserts and deletes elements at the inserts and deletes elements at the rear of the list.rear of the list.
Operations at Intermediate Operations at Intermediate positions have O(n) efficiency.positions have O(n) efficiency.
Link ListLink List
Elements have a value and links that Elements have a value and links that identify adjacent elements in the identify adjacent elements in the sequence.sequence.
Inserting or deleting elements are Inserting or deleting elements are O(1) operations.O(1) operations.
Link list vs. Array ListLink list vs. Array List
Link List Array Link List Array ListList
1. No random access 1. Fast 1. No random access 1. Fast random accessrandom access
2. Fast Manipulation 2.Slow 2. Fast Manipulation 2.Slow manipulationmanipulation
Set InterfaceSet Interface
Set also extends Set also extends CollectionCollection, but it , but it prohibits duplicate items (this is prohibits duplicate items (this is what defines a Set).what defines a Set).
No new methods are introduced.No new methods are introduced. Concrete Set implementations Concrete Set implementations
contain methods that forbid adding contain methods that forbid adding two equal Objectstwo equal Objects..
HashSets Vs Tree SetHashSets Vs Tree Set
Hash Set Tree SetHash Set Tree SetStorage method: hash table red Storage method: hash table red
black treeblack treeSpace used: O(n) O(n)Space used: O(n) O(n)Put speed: O(1) O(lg n) Put speed: O(1) O(lg n) Iteration order: Arbitrary SortedIteration order: Arbitrary Sorted
Rule of thumb: Use a Tree only if you need Rule of thumb: Use a Tree only if you need them sorted, otherwise use a Hashthem sorted, otherwise use a Hash
MapsMaps
Maps are similar to collections but are Maps are similar to collections but are actually represented by an entirely actually represented by an entirely different class hierarchy.different class hierarchy.
Maps store objects by Maps store objects by key/valuekey/value pairs. pairs. Keys may not be duplicated.Keys may not be duplicated. Each key may map to only one value.Each key may map to only one value.
Map ImplementationsMap Implementations
Java provides several common class Java provides several common class implementations:implementations: HashMapHashMap
A hashtable implementation of a map.A hashtable implementation of a map. Good for quick searching where order Good for quick searching where order
doesn’t matter.doesn’t matter. TreeMapTreeMap
A tree implementation of a map.A tree implementation of a map. Good when natural ordering is required.Good when natural ordering is required.
HashMap Vs Tree MapHashMap Vs Tree Map
Hash map Tree Hash map Tree mapmap
Storage method: hash table red Storage method: hash table red black treeblack tree
Space used: O(n) O(n)Space used: O(n) O(n)Put speed: O(1) O(lg n) Put speed: O(1) O(lg n) Iteration order: Arbitrary SortedIteration order: Arbitrary Sorted
Rule of thumb: Use a Tree only if you need Rule of thumb: Use a Tree only if you need them sorted, otherwise use a Hashthem sorted, otherwise use a Hash
Binary Search Tree
StartSmall amount of
data?
linked list
Balanced Tree
Hash Table
OrderedArray
Amount of data predictabl
e?
Searching and insertion must be very
fast? Search
speed more
important then
insertion speed ?
Key distribu
tion guaran
teed random
?
yes
Moreadditi
on deletion?
Array list
yes
yes
yes
yes
yes
No
No
No
No
No
No
UnorderedArray
Data StructuresData StructuresAdvantagesAdvantagesDisadvantagesDisadvantages
Array Array Quick Quick insertion, very insertion, very fast access if fast access if index known. index known.
Slow search, Slow search, slow deletion, slow deletion, and fixed size. and fixed size.
Ordered Ordered arrayarray
Quicker search Quicker search than unsorted than unsorted
array array
Slow insertion Slow insertion and deletion, and deletion,
fixed size fixed size
Stack Stack Last in first Last in first out out
Slow access to Slow access to other items other items
QueueQueue First in first First in first out access. out access.
Slow access to Slow access to other items other items
Linked listLinked list Quick Quick insertion, insertion,
quick deletion quick deletion
Slow search Slow search
Array List Array List Random Random AccessAccess
Slow insertion, Slow insertion, deletion deletion
Thank youThank you