1 / 23

Collections

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.

paulina
Download Presentation

Collections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Collections Data structures in Java

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

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

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

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

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

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

  8. Interfaces Collection  Set, List • A group of objects. • May or may not be ordered; • May or may not contain duplicates.

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

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

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

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

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

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

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

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

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

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

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

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

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

  22. Thank you

More Related