23
Collection Collection s s Data structures in Java Data structures in Java

Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

Embed Size (px)

Citation preview

Page 1: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

CollectionsCollections

Data structures in JavaData structures in Java

Page 2: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

OBJECTIVEOBJECTIVE

“ “ WHEN TO USE WHICH DATA WHEN TO USE WHICH DATA STRUCTURESTRUCTURE ””

Debug

Page 3: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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.

Page 4: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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

Page 5: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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..

Page 6: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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.

Page 7: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

Interfaces and Interfaces and implementationimplementation

Collection

List

Set

SortedSet

Map

SortedMap

LinkedList ArrayList

HashSet

TreeSet

HashMap

TreeMap

Extends

Implements

Interface

Class

Page 8: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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.

Page 9: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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).

Page 10: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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

Page 11: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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..

Page 12: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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.

Page 13: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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.

Page 14: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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.

Page 15: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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

Page 16: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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..

Page 17: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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

Page 18: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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.

Page 19: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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.

Page 20: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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

Page 21: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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

Page 22: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

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

Page 23: Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

Thank youThank you