Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g."— Presentation transcript:

1 Collections Data structures in Java

2 OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g

3 Data Structures 1. A data structure is an arrangement of data in a computer’s memory. 1. A data structure is an arrangement of data in a computer’s memory. 2. It includes list,stack,binary trees, hash tables, etc. 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. 3. Algorithms manipulate the data in these structures in various ways such as searching and sorting.

4 What is a Collections framework? A framework is an extensive set of interfaces, abstract classes and concrete classes together with support tools 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: Framework is provided in java.util package & comprises three parts: 1. Core interfaces 1. Core interfaces 2. Set of implementations. 2. Set of implementations. 3. Utility methods 3. Utility methods

5 Need of a framework???? Pre Java SDK1.2, Java provided a handful of data structures: Pre Java SDK1.2, Java provided a handful of data structures: Hash Table Hash Table Vector Vector Stack Stack These were good and easy to use, but they were not organized into a more general framework. These were good and easy to use, but they were not organized into a more general framework. Lacked interoperability. Lacked interoperability.

6 Features of Collection framework Reduces programming effort. Reduces programming effort. Increases performance. Increases performance. Provides interoperability between unrelated APIs. Provides interoperability between unrelated APIs. Faster software reuse. Faster software reuse.

7 Interfaces and implementation Collection List Set SortedSet Map SortedMap LinkedListArrayList HashSet TreeSet HashMap TreeMap Extends Implements Interface Class

8 Interfaces Collection  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.

9 Interface continued Set  Set  The familiar set abstraction. The familiar set abstraction. No duplicates; May or may not be ordered. No 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 access Duplicates 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 (function). Each key can map to at most one value (function).

10 Iterators A collection provides an iterator which allows sequential access to the elements of a collection. A collection provides an iterator which allows sequential access to the elements of a collection. Methods: Methods: has Next() – check if there are still elements has Next() – check if there are still elements next() – return the next object and advance next() – return the next object and advance remove() – remove the currently pointed object remove() – remove the currently pointed object

11 List interface An interface that extends the Collections interface. An interface that extends the Collections interface. An ordered collection. An ordered collection. Stores element by position Stores element by position Includes index based operation. Includes index based operation. Allows duplicate elements. Allows duplicate elements.

12 Concrete List Implementations There are two concrete implementations of the List interface There are two concrete implementations of the List interface LinkedList LinkedList ArrayList ArrayList Which is best to use depends on specific needs. Which is best to use depends on specific needs.

13 Array List Stores element in a contiguous block of memory and automatically expandable. 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. The collection efficiently (O(1)) inserts and deletes elements at the rear of the list. Operations at Intermediate positions have O(n) efficiency. Operations at Intermediate positions have O(n) efficiency.

14 Link List Elements have a value and links that identify adjacent elements in the sequence. Elements have a value and links that identify adjacent elements in the sequence. Inserting or deleting elements are O(1) operations. Inserting or deleting elements are O(1) operations.

15 Link list vs. Array List Link List Array List Link List Array List 1. No random access 1. Fast random access 1. No random access 1. Fast random access 2. Fast Manipulation 2.Slow manipulation 2. Fast Manipulation 2.Slow manipulation

16 Set Interface Set also extends Collection, but it prohibits duplicate items (this is what defines a Set). Set also extends Collection, but it prohibits duplicate items (this is what defines a Set). No new methods are introduced. No new methods are introduced. Concrete Set implementations contain methods that forbid adding two equal Objects. Concrete Set implementations contain methods that forbid adding two equal Objects.

17 HashSets Vs Tree Set Hash Set 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

18 Maps Maps are similar to collections but are actually represented by an entirely different class hierarchy. Maps are similar to collections but are actually represented by an entirely different class hierarchy. Maps store objects by key/value pairs. Maps store objects by key/value 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.

19 Map Implementations Java provides several common class implementations: Java provides several common class implementations: HashMap HashMap A hashtable implementation of a map. A hashtable implementation of a map. Good for quick searching where order doesn’t matter. Good for quick searching where order doesn’t matter. TreeMap TreeMap A tree implementation of a map. A tree implementation of a map. Good when natural ordering is required. Good when natural ordering is required.

20 HashMap Vs Tree Map Hash map 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

21 Binary Search Tree Start Small amou nt of data ? linked list Balanced Tree Hash Table Ordered Array Amou nt of data predic table? Searchin g and insertion must be very fast? Search speed more important then insertion speed ? Key distribut ion guarante ed random? yes More additi on deleti on? Array list yes No Unordered Array

22 Data Structures AdvantagesDisadvantages Array Quick insertion, very fast access if index known. Slow search, slow deletion, and fixed size. Ordered array Quicker search than unsorted array Slow insertion and deletion, fixed size Stack Last in first out Slow access to other items Queue First in first out access. Slow access to other items Linked list Quick insertion, quick deletion Slow search Array List Random Access Slow insertion, deletion

23 Thank you


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

Similar presentations


Ads by Google