Session Outcome
At the end of the session, student is able to:
 Understand what is collection frameworks .
 Understand the types of collection frameworks.
 Understand its usage.
KLEF                          OOPS                 BES-1
          Session Objective
 • Java Collection plays a major role to provides many
   interfaces (Set, List, Queue, Deque) and classes (Array
   List, Vector, Linked List, Priority Queue, Hash Set,
   Linked Hash Set, Tree Set).
KLEF                      OOPS                        BES-1
           Need of the Topic
 • Collection framework is used to organize multiple
   objects in a program,
 • Using Java Collections it is possible to achieve all the
   operations that can perform on a data such as
   searching, sorting, insertion, manipulation, and deletion.
KLEF                       OOPS                      BES-1
   Collections Framework Diagram
 Each collection class implements an interface from a
 hierarchy
       Each class is designed for a
       specific type of storage
KLEF                           OOPS            BES-1
                   Lists and Sets
 Ordered Lists
       Array List
          Stores a list of items in a dynamically sized array
       Linked List
          Allows speedy insertion and removal of items from the
          list
                            A list is a collection that maintains the
                            order of its elements.
KLEF                         OOPS                                BES-1
                  Lists and Sets
• Unordered Sets
  – Hash Set
       • Uses hash tables to speed up finding, adding, and
         removing elements
  – Tree Set
       • Uses a binary tree to speed up finding, adding,
         and removing elements
                             A set is an unordered collection of
                             unique elements.
KLEF                       OOPS                                    BES-1
                 Stacks and Queues
Another way of gaining efficiency in a collection is to
reduce the number of operations available
Two examples are:
   Stack
       Remembers the order of its elements, but it does not allow
       you to insert elements in every position
       You can only add and remove elements at the top
   Queue
       Add items to one end (the tail)
       Remove them from the other end (the head)
       Example: A line of people waiting for a bank teller
KLEF                          OOPS                           BES-1
                              Maps
       A map keeps associations
       between key and value objects.
• A map stores keys, values, and the associations
  between them
  – Example:
  – Barcode keys and books
• Keys
  – Provides an easy way to represent an object (such as a
    numeric bar code, or a Student Identification Number)
• Values
  – The actual object that is associated with the key
KLEF                              OOPS              BES-1
                  Linked Lists
• Linked lists use references to maintain an
  ordered lists of ‘nodes’
  – The ‘head’ of the list references the first node
  – Each node has a value and a reference to the next
    node
  – They can be used to implement
     • A List Interface
     • A Queue Interface
KLEF                    OOPS                     BES-1
Linked Lists Operations
  – Insertion of a node
       • Find the elements it goes between
       • Remap the references
  – Removal of a node
       • Find the element to remove
       • Remap neighbor’s references
                              Each instance variable is declared just like
                              other variables we have used.
KLEF                          OOPS                               BES-1
       Linked List: Few Sample methods
KLEF                 OOPS                BES-1
               Generic Linked Lists
• The Collection Framework uses Generics
  – Each list is declared with a type field in < > angle brackets
   LinkedList<String> employeeNames = . . .;
   LinkedList<String>
   LinkedList<Employee>
KLEF                        OOPS                            BES-1
                      List Iterators
   When traversing a LinkedList, use ListIterator
       Keeps track of where you are in the list.
ListIterator<String> iter employeeNames.listIterator()
   An iterator is used to :
        Access elements inside a linked list
        Visit other than the first and the last nodes
KLEF                          OOPS                       BES-1
       Iterator and ListIterator Methods
 Iterators allow you to move through a list easily
                     Similar to an index variable for an array
                                                  NULL
KLEF                    OOPS                             BES-1
                         Sets
 A set is an unordered collection
    It does not support duplicate elements
 The collection does not keep track of the order in
 which elements have been added
    Therefore, it can carry out its operations more efficiently than
    an ordered collection
                          The HashSet and TreeSet classes both
                          implement the Set interface.
KLEF                         OOPS                         BES-1
                              Sets
• HashSet: Stores data in a Hash Table
• TreeSet: Stores data in a Binary Tree
• Both implementations arrange the set
  elements so that finding, adding, and
  removing elements is efficient
 Set implementations arrange the elements so that they can locate them
 quickly
KLEF                              OOPS                              BES-1
             Tree Concept
• Set elements are kept in sorted order
  – Nodes are not arranged in a linear sequence
    but in a tree shape.
  – In order to use a TreeSet, it must be possible to
    compare the elements and determine which one
    is “larger”
KLEF                   OOPS                  BES-1
                   TreeSet
 TreeSet is used for classes that implement
  the Comparable interface
   • String and Integer, for example
   • The nodes are arranged in a ‘tree’ fashion so that
     each ‘parent’ node has two child nodes.
       – The node to the left always has a ‘smaller’ value
       – The node to the right always has a ‘larger’ value
 Set<String> names = new TreeSet<String>();
KLEF                     OOPS                         BES-1
                        Maps
  A map allows you to associate elements from a key
  set with elements from a value collection.
       The HashMap and TreeMap classes both implement the
       Map interface.
       Use a map to look up objects by using a key
KLEF                         OOPS                    BES-1
       Working with Maps
KLEF           OOPS        BES-1
    Key Value Pairs in Maps
 Each key is associated with a value
Map<String, Color>
Map<String, Color> favoriteColors
                   favoriteColors == new
                                     new HashMap<String,
                                         HashMap<String, Color>();
                                                         Color>();
favoriteColors.put("Juliet", Color.RED);
favoriteColors.put("Juliet",  Color.RED);
favoriteColors.put(“Romeo", Color.GREEN);
favoriteColors.put(“Romeo",  Color.GREEN);
Color julietsFavoriteColor
Color julietsFavoriteColor == favoriteColors.get("Juliet");
                              favoriteColors.get("Juliet");
favoriteColors.remove("Juliet");
favoriteColors.remove("Juliet");
KLEF                         OOPS                            BES-1
       Example Program
KLEF         OOPS        BES-1