Java Collections
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
19,679
On Slideshare
19,276
From Embeds
403
Number of Embeds
14

Actions

Shares
Downloads
528
Comments
1
Likes
9

Embeds 403

http://www.techgig.com 118
http://www.indijava.in 103
http://indijava.in 78
http://www.slideshare.net 47
http://hpfloresj.blogspot.com 29
http://localhost 10
http://www.techiegyan.com 5
http://www.slideshow.com 4
http://www.e-presentations.us 4
http://slideshow.com 1
http://www.dvd-photo.slideshow.com 1
http://scitjava.blogspot.com 1
http://dev.toeska.cl 1
http://192.168.2.25 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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