250 likes | 304 Views
Collections. Data structures in Java. g. u. b. e. D. OBJECTIVE. “ WHEN TO USE WHICH DATA STRUCTURE ”. Data Structures. 1. A data structure is an arrangement of data in a computer’s memory. 2. It includes list,stack,binary trees, hash tables, etc.
E N D
Collections Data structures in Java
g u b e D OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE”
Data Structures 1. A data structure is an arrangement of data in a computer’s memory. 2. It includes list,stack,binary trees, hash tables, etc. 3. Algorithms manipulate the data in these structures in various ways such as searching and sorting.
What is a Collections framework? • A framework is an extensive set of interfaces, abstract classes and concrete classes together with support tools • Framework is provided in java.util package & comprises three parts: 1. Core interfaces 2. Set of implementations. 3. Utility methods
Need of a framework???? • Pre Java SDK1.2, Java provided a handful of data structures: • Hash Table • Vector • Stack • These were good and easy to use, but they were not organized into a more general framework. • Lacked interoperability.
Features of Collection framework • Reduces programming effort. • Increases performance. • Provides interoperability between unrelated APIs. • Faster software reuse.
Extends Collection Map Implements Interface Class Set HashMap SortedMap List HashSet SortedSet LinkedList ArrayList TreeMap TreeSet Interfaces and implementation
Interfaces Collection Set, List • A group of objects. • May or may not be ordered; • May or may not contain duplicates.
Interface continued • Set • The familiar set abstraction. • No duplicates; May or may not be ordered. • List • Ordered collection, also known as a sequence. • Duplicates permitted; Allows positional access • Map • A mapping from keys to values. • Each key can map to at most one value (function).
Iterators • A collection provides an iterator which allows sequential access to the elements of a collection. • Methods: • has Next() – check if there are still elements • next() – return the next object and advance • remove() – remove the currently pointed object
List interface • An interface that extends the Collections interface. • An ordered collection . • Stores element by position • Includes index based operation. • Allows duplicate elements.
Concrete List Implementations • There are two concrete implementations of the List interface • LinkedList • ArrayList • Which is best to use depends on specific needs.
Array List • Stores element in a contiguous block of memory and automatically expandable. • The collection efficiently (O(1)) inserts and deletes elements at the rear of the list. • Operations at Intermediate positions have O(n) efficiency.
Link List • Elements have a value and links that identify adjacent elements in the sequence. • Inserting or deleting elements are O(1) operations.
Link list vs. Array List Link List Array List 1. No random access 1. Fast random access 2. Fast Manipulation 2.Slow manipulation
Set Interface • Set also extends Collection, but it prohibits duplicate items (this is what defines a Set). • No new methods are introduced. • Concrete Set implementations contain methods that forbid adding two equal Objects.
HashSets Vs Tree Set Hash Set Tree Set Storage method: hash table red black tree Space used: O(n) O(n) Put speed: O(1) O(lg n) Iteration order: Arbitrary Sorted Rule of thumb: Use a Tree only if you need them sorted, otherwise use a Hash
Maps • Maps are similar to collections but are actually represented by an entirely different class hierarchy. • Maps store objects by key/value pairs. • Keys may not be duplicated. • Each key may map to only one value.
Map Implementations • Java provides several common class implementations: • HashMap • A hashtable implementation of a map. • Good for quick searching where order doesn’t matter. • TreeMap • A tree implementation of a map. • Good when natural ordering is required.
HashMap Vs Tree Map Hash map Tree map Storage method: hash table red black tree Space used: O(n) O(n) Put speed: O(1) O(lg n) Iteration order: Arbitrary Sorted Rule of thumb: Use a Tree only if you need them sorted, otherwise use a Hash
Small amount of data? More addition deletion? Amount of data predictable? Start linked list No yes yes No No Searching and insertion must be very fast? yes Array list Hash Table yes Search speed more important then insertion speed ? yes Ordered Array No Key distribution guaranteed random? Binary Search Tree No yes Unordered Array No Balanced Tree