Java Collections


Published on

Published in: Business, Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Java Collections

    1. 1. Java Collections <ul><ul><li>Parag Shah </li></ul></ul><ul><ul><li>Adaptive Software Solutions </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul>
    2. 2. Agenda <ul><li>Collection classes in Java </li></ul><ul><ul><li>New </li></ul></ul><ul><ul><li>Legacy </li></ul></ul><ul><li>Iterating through collections </li></ul>
    3. 3. Introduction <ul><li>The need for holding objects </li></ul><ul><li>Collection support in Java </li></ul><ul><ul><li>Array </li></ul></ul><ul><ul><li>Vector </li></ul></ul><ul><ul><li>Hashmap </li></ul></ul><ul><ul><li>List </li></ul></ul><ul><ul><li>Set </li></ul></ul><ul><ul><li>Map </li></ul></ul>
    4. 4. Array <ul><li>Arrays are first class objects </li></ul><ul><li>Advantages of using Arrays </li></ul><ul><ul><li>Efficient </li></ul></ul><ul><ul><li>Type of objects determined at compile time </li></ul></ul><ul><ul><li>Can store primitives directly </li></ul></ul><ul><li>Disadvantages of using Arrays </li></ul><ul><ul><li>The size is fixed </li></ul></ul><ul><ul><li>The type of objects it can store is fixed </li></ul></ul>
    5. 5. The Java Collections Library <ul><li>Basic Collection Interfaces </li></ul><ul><ul><li>Collection </li></ul></ul><ul><ul><ul><li>List </li></ul></ul></ul><ul><ul><ul><li>Set </li></ul></ul></ul><ul><ul><li>Map </li></ul></ul>
    6. 6. Basic Collection Classes <ul><li>Basic Collection Classes </li></ul>
    7. 7. Collection Classes (contd.) <ul><li>Classes that work with Collections </li></ul>
    8. 8. Collection Classes Overview <ul><li>Collection: Stores a collection of objects, such that a rule is applied to the objects </li></ul><ul><ul><li>List: Stores objects in a certain order </li></ul></ul><ul><ul><ul><li>Implemented By </li></ul></ul></ul><ul><ul><ul><ul><li>ArrayList </li></ul></ul></ul></ul><ul><ul><ul><ul><li>LinkedList </li></ul></ul></ul></ul><ul><ul><li>Set: Stores only unique objects </li></ul></ul><ul><ul><ul><li>Implemented By </li></ul></ul></ul><ul><ul><ul><ul><li>HashSet </li></ul></ul></ul></ul><ul><ul><ul><ul><li>TreeSet </li></ul></ul></ul></ul><ul><li>Map: Stores key-value pairs </li></ul><ul><ul><ul><li>Implemented By </li></ul></ul></ul><ul><ul><ul><ul><li>HashMap </li></ul></ul></ul></ul><ul><ul><ul><ul><li>TreeMap </li></ul></ul></ul></ul><ul><ul><ul><ul><li>WeakHashMap </li></ul></ul></ul></ul>
    9. 9. Collection Interface <ul><li>java.util.Collection </li></ul><ul><ul><li>Root Interface for the Collection hierarchy in Java </li></ul></ul><ul><ul><li>Holds a group of objects which adhere to some rule </li></ul></ul><ul><ul><li>Classes implementing this Interface are suggested to have atleast 2 constructors: </li></ul></ul><ul><ul><ul><li>A no-arg constructor </li></ul></ul></ul><ul><ul><ul><li>A constructor which takes a collection of objects </li></ul></ul></ul>
    10. 10. List <ul><li>java.util.List </li></ul><ul><ul><li>A List is an ordered collection of objects </li></ul></ul><ul><ul><ul><li>Implemented by ArrayList, and LinkedList </li></ul></ul></ul><ul><ul><li>Users have control on where in the List, new elements should be added </li></ul></ul><ul><ul><li>Lists can contain duplicates as determined by the equals() method </li></ul></ul><ul><ul><li>Use caution while adding a List element to a List </li></ul></ul><ul><ul><ul><li>equals(), and hashCode() may not work properly </li></ul></ul></ul>
    11. 11. ArrayList <ul><li>See [] </li></ul><ul><li>java.util.ArrayList </li></ul><ul><ul><li>Resizable array implementation of the List Interface </li></ul></ul><ul><ul><li>Adding elements require O(n) time </li></ul></ul><ul><ul><li>Other operations are performed linear time </li></ul></ul><ul><ul><li>The capacity of ArrayLists can be increased before adding a large number of elements </li></ul></ul><ul><ul><li>ArrayList is NOT synchronized </li></ul></ul><ul><ul><li>Inserting elements in the list is inefficient </li></ul></ul>
    12. 12. LinkedList <ul><li>See [] </li></ul><ul><li>java.util.LinkedList </li></ul><ul><ul><li>Doubly Linked List implementation of the List Interface </li></ul></ul><ul><ul><li>Provides special methods to get, remove, and insert elements at the beginning and the end of the list </li></ul></ul><ul><ul><ul><li>Enables the usage of a LinkedList as: </li></ul></ul></ul><ul><ul><ul><ul><li>Queues </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Stacks </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Deque </li></ul></ul></ul></ul><ul><ul><li>Inefficient for random access </li></ul></ul><ul><ul><li>Very efficient for sequential access and insertions in the middle of the list </li></ul></ul><ul><ul><li>LinkedList's are not synchronized </li></ul></ul>
    13. 13. Set <ul><li>java.util.Set </li></ul><ul><ul><li>A Set is a Collection that does not contain any duplicate elements </li></ul></ul><ul><ul><li>Elements that are put in a Set must override the equals() method to establish uniqueness </li></ul></ul><ul><ul><li>A Set cannot contain itself as an element </li></ul></ul><ul><ul><li>Caution must be exercised when adding a mutable element to a set </li></ul></ul>
    14. 14. HashSet <ul><li>See [] </li></ul><ul><li>java.util.HashSet </li></ul><ul><ul><li>A Set backed by a HashMap </li></ul></ul><ul><ul><li>Can contain nulls </li></ul></ul><ul><ul><li>Basic operations take constant time </li></ul></ul><ul><ul><li>Iteration order may not always be the same </li></ul></ul><ul><ul><li>Used for Sets where lookup time has to be optimized </li></ul></ul><ul><ul><li>User defined classes must override the hashCode() method </li></ul></ul><ul><ul><li>Operations not synchronized </li></ul></ul>
    15. 15. TreeSet <ul><li>See [] </li></ul><ul><li>java.util.TreeSet </li></ul><ul><ul><li>A Set backed by a TreeMap </li></ul></ul><ul><ul><li>The sorted Set is gauranteed to be in ascending order, sorted by the natural order of the elements or by the comparator provided </li></ul></ul><ul><ul><li>User defined classes must implement the Comparable Interface </li></ul></ul><ul><ul><li>A TreeSet is not synchronized </li></ul></ul>
    16. 16. Map <ul><li>java.util.Map </li></ul><ul><ul><li>It is an associative array </li></ul></ul><ul><ul><li>Used to lookup objects based on keys </li></ul></ul><ul><ul><li>Implemented by the HashMap and TreeMap classes </li></ul></ul>
    17. 17. HashMap <ul><li>See [] </li></ul><ul><li>java.util.HashMap </li></ul><ul><ul><li>Implementation is based on a Hashtable </li></ul></ul><ul><ul><li>Ideal for fast lookups </li></ul></ul><ul><ul><li>Provides constant time performance for inserting and locating pairs </li></ul></ul><ul><ul><li>Allows null for keys and values </li></ul></ul><ul><ul><li>Methods are not synchronized </li></ul></ul>
    18. 18. TreeMap <ul><li>See [] </li></ul><ul><li>java.util.TreeMap </li></ul><ul><ul><li>Implemented based on Red-Black trees </li></ul></ul><ul><ul><li>The keys appear sorted </li></ul></ul><ul><ul><li>Basic operations take log(n) time </li></ul></ul><ul><ul><li>The ordering maintained by HashMap must be consistent with equals() </li></ul></ul><ul><ul><li>Methods are not synchronized </li></ul></ul>
    19. 19. Iterators <ul><li>Iterators provide the ability to traverse through a sequence of elements without knowing the underlying data structure of the sequence </li></ul><ul><li>Iterator support in Java </li></ul><ul><ul><li>Interface Iterator </li></ul></ul><ul><ul><ul><li>Allows unidirectional traversal and removal of elements from the underlying collection </li></ul></ul></ul><ul><ul><li>Interface ListIterator </li></ul></ul><ul><ul><ul><li>Allows bi-directional traversal as well as addition, removal, & substitution of elements in the underlying collection </li></ul></ul></ul>
    20. 20. Using Iterators <ul><li>Always use iterators and not for loops while iterating through collections </li></ul><ul><li>Use Iterator to iterate and remove elements from a collection </li></ul><ul><li>ListIterator provides more features </li></ul><ul><ul><li>Allows insertion & replacement of elements </li></ul></ul><ul><ul><li>Allows bi-directional navigation </li></ul></ul><ul><li>See [] </li></ul><ul><li>See [] </li></ul>
    21. 21. Summary <ul><li>Revise various collection interfaces and their implementations </li></ul><ul><li>Discuss pros and cons of choosing the collection type </li></ul><ul><li>Discuss legacy collection types </li></ul>
    22. 22. Where to Get More Information <ul><li>Bruce Eckel's – Thinking in Java </li></ul><ul><li>Java tutorial </li></ul>