5.2_CollectionFramework

download 5.2_CollectionFramework

of 29

Transcript of 5.2_CollectionFramework

  • 8/6/2019 5.2_CollectionFramework

    1/29

    Collections Framework

  • 8/6/2019 5.2_CollectionFramework

    2/29

    2

    ContentsContents

    What is Collection?

    Collections Framework

    Collections Hierarchy

    Collections Implementations

    Set

    List

    Map

  • 8/6/2019 5.2_CollectionFramework

    3/29

    3

    ObjectivesObjectives

    Define a collection

    Describe the collections framework

    Describe the collections hierarchy

    Demonstrate each collection implementation

  • 8/6/2019 5.2_CollectionFramework

    4/29

    4

    What is a Collection?What is a Collection?

    A Collection (also known as container) is an object that contains a

    group of objects treated as a single unit.

    Any type of objects can be stored, retrieved and manipulated as

    elements of collections.

  • 8/6/2019 5.2_CollectionFramework

    5/29

    5

    Collections FrameworkCollections Framework

    Collections Frameworkis a unified architecture formanaging collections

    Main Parts of Collections Framework

    1. Interfaces Core interfaces defining common functionality exhibited by

    collections

    2. Implementations

    Concrete classes of the core interfaces providing datastructures

    3. Operations Methods that perform various operations on collections

  • 8/6/2019 5.2_CollectionFramework

    6/29

    6

    Collections Framework

    InterfacesCollections Framework

    Interfaces

    Core Interface Description

    Collection specifies contract that all collections should implement

    Set defines functionality for a setof unique elements

    SortedSet defines functionality for a setwhere elements are sorted

    List defines functionality for an ordered listof non- unique elements

    Map defines functionality for mapping of unique keys to values

    SortedMap defines functionality for a map where its keys are sorted

  • 8/6/2019 5.2_CollectionFramework

    7/29

    7

    Collections Framework

    ImplementationsCollections Framework

    Implementations

    Set List Map

    HashSet ArrayList HashMap

    LinkedHashSet LinkedList LinkedHashMap

    TreeSet Vector Hashtable

    TreeMap

    Note: Hashtable uses a lower-case t

  • 8/6/2019 5.2_CollectionFramework

    8/29

    8

    Collections Framework

    OperationsCollections Framework

    Operations

    Basic collection operations:

    Check if collection is empty

    Check if an object exists in collection.

    Retrieve an object from collection

    Add object to collection Remove object from collection

    Iterate collection and inspect each object

    Each operation has a corresponding method implementation for eachcollection type

  • 8/6/2019 5.2_CollectionFramework

    9/29

    9

    Collections CharacteristicsCollections Characteristics

    Ordered

    Elements are stored and accessed in a specific order

    Sorted

    Elements are stored and accessed in a sorted order Indexed

    Elements can be accessed using an index

    Unique

    Collection does not allow duplicates

  • 8/6/2019 5.2_CollectionFramework

    10/29

    10

    IteratorIterator

    An iterator is an object used to mark a position in a

    collection of data and to move from item to item within the

    collection

    Syntax:Iterator = .iterator();

  • 8/6/2019 5.2_CollectionFramework

    11/29

    11

    Collections Hierarchy

    Set and ListCollections Hierarchy

    Set and List

    HashSet

    Collection

    SortedSet

    ListSet

    LinkedHashSet TreeSet LinkedList Vector ArrayList

    implements

    implements

    implements

    implements extends

    extends

  • 8/6/2019 5.2_CollectionFramework

    12/29

    12

    Collections Hierarchy

    MapCollections Hierarchy

    Map

    Map

    SortedMap

    Hashtable

    LinkedHashMap

    HashMap TreeMap

    implements

    implements

    extends

    extends

  • 8/6/2019 5.2_CollectionFramework

    13/29

    13

    Set : Unique things (classes that implement Set)

    Map : Things with a unique ID (classes that implementMap)

    List : Lists of things (classes that implement List)

    Collection ImplementationsCollection Implementations

    Next!

  • 8/6/2019 5.2_CollectionFramework

    14/29

  • 8/6/2019 5.2_CollectionFramework

    15/29

    15

    List Implementations

    ArrayListList Implementations

    ArrayList

    import java.util.ArrayList;

    public class MyArrayList {

    public static voidmain(String args[ ]) {

    ArrayList alist = newArrayList( );

    alist.add(new String("One"));

    alist.add(new String("Two"));

    alist.add(new String("Three"));

    System.out.println(alist.get(0));

    System.out.println(alist.get(1));

    System.out.println(alist.get(2));

    }}

    Back!

    One

    Two

    Three

  • 8/6/2019 5.2_CollectionFramework

    16/29

    16

    List Implementations

    VectorList Implementations

    Vector

    import java.util.Vector;

    public class MyVector {

    public static voidmain(String args[ ]) {

    Vector vecky = newVector( );

    vecky.add(new Integer(1));

    vecky.add(new Integer(2));

    vecky.add(new Integer(3));

    for(int x=0; x

  • 8/6/2019 5.2_CollectionFramework

    17/29

    17

    List Implementations

    LinkedListList Implementations

    LinkedList

    import java.util.LinkedList;

    public class MyLinkedList {

    public static voidmain(String args[ ]) {

    LinkedList link = new LinkedList( );

    link.add(new Double(2.0));

    link.addLast(new Double(3.0));

    link.addFirst(new Double(1.0));

    Object array[ ] = link.toArray( );

    for(int x=0; x

  • 8/6/2019 5.2_CollectionFramework

    18/29

    18

    A Set cares about uniqueness, it doesnt

    allow duplicates.

    SetSet

    Paul

    Mark

    John Luke

    FredPeter

    TreeSetLinkedHashSetHashSet

  • 8/6/2019 5.2_CollectionFramework

    19/29

    19

    Set Implementations

    HashSetSet Implementations

    HashSet

    import java.util.*;

    public class MyHashSet {

    public static voidmain(String args[ ]) {

    HashSet hash = new HashSet( );

    hash.add("a");

    hash.add("b");

    hash.add("c");

    hash.add("d");

    Iterator iterator = hash.iterator( );

    while(iterator.hasNext( )) {System.out.println(iterator.next( ));

    }

    }

    }

    Back!

    d

    a

    c

    b

  • 8/6/2019 5.2_CollectionFramework

    20/29

    20

    Set Implementations

    LinkedHashSetSet Implementations

    LinkedHashSet

    import java.util.LinkedHashSet;

    public class MyLinkedHashSet {

    public static void main(String args[ ]) {

    LinkedHashSet lhs = new LinkedHashSet();

    lhs.add(new String("One"));

    lhs.add(new String("Two"));

    lhs.add(new String("Three"));

    Object array[] = lhs.toArray( );

    for(int x=0; x

  • 8/6/2019 5.2_CollectionFramework

    21/29

    21

    Set Implementations

    TreeSetSet Implementations

    TreeSet

    import java.util.TreeSet;

    import java.util.Iterator;

    public class MyTreeSet {

    public static voidmain(String args[ ]) {

    TreeSet tree = new TreeSet();

    tree.add("Jody");

    tree.add("Remiel");

    tree.add("Reggie");

    tree.add("Philippe");

    Iterator iterator = tree.iterator( );

    while(iterator.hasNext( )) {

    System.out.println(iterator.next( ).toString( ));

    }

    }

    }

    Back!

    Jody

    Philippe

    Reggie

    Remiel

  • 8/6/2019 5.2_CollectionFramework

    22/29

    22

    A Map cares about unique identifiers.

    MapMap

    Paul Mark John Paul Luke

    key

    value

    Pl Ma Jn ul Le

    LinkedHashMap TreeMapHashtableHashMap

  • 8/6/2019 5.2_CollectionFramework

    23/29

  • 8/6/2019 5.2_CollectionFramework

    24/29

    24

    Map Implementations

    HashtableMap Implementations

    Hashtable

    import java.util.Hashtable;

    public class MyHashtable {

    public static voidmain(String args[ ]) {

    Hashtable table = new Hashtable( );

    table.put("name", "Jody");table.put("id", new Integer(1001));

    table.put("address", new String("Manila"));

    System.out.println("Table of Contents:" + table);

    }

    }

    Back!

    Table of Contents:

    {address=Manila, name=Jody, id=1001}

  • 8/6/2019 5.2_CollectionFramework

    25/29

    25

    Map Implementations

    LinkedHashMapMap Implementations

    LinkedHashMap

    import java.util.*;

    public class MyLinkedHashMap {

    public static voidmain(String args[ ]) {

    int iNum = 0;

    LinkedHashMap myMap = new LinkedHashMap( );

    myMap.put("name", "Jody");

    myMap.put("id", new Integer(446));

    myMap.put("address", "Manila");

    myMap.put("type", "Savings");

    Collection values = myMap.values( );

    Iterator iterator = values.iterator( );

    while(iterator.hasNext()) {

    System.out.println(iterator.next( ));

    }

    }

    }

    Back!

    Jody446

    Manila

    Savings

  • 8/6/2019 5.2_CollectionFramework

    26/29

    26

    Map Implementations

    TreeMapMap Implementations

    TreeMap

    import java.util.*;

    public class MyTreeMap {

    public static voidmain(String args[]) {

    TreeMap treeMap = new TreeMap( );

    treeMap.put("name", "Jody");

    treeMap.put("id", new Integer(446));

    treeMap.put("address", "Manila");

    Collection values = treeMap.values()

    Iterator iterator = values.iterator( );

    System.out.println("Printing the VALUES....");

    while (iterator.hasNext()) {

    System.out.println(iterator.next( ));

    }

    }

    }

    Back!

    Printing the VALUES....

    Manila

    446

    Jody

  • 8/6/2019 5.2_CollectionFramework

    27/29

    27

    Collection Classes SummaryCollection Classes Summary

    NoBy indexXLinkedList

    NoBy indexXVector

    NoBy indexXArrayList

    NoBy insertion order orlast access order

    XLinkedHashSet

    By natural orderor

    custom comparison rules

    SortedXTreeSet

    NoNoXHashSet

    NoBy insertion order or

    last access order

    XLinkedHashMap

    By natural orderor

    custom comparison rules

    SortedXTreeMap

    NoNoXHashtable

    NoNoXHashMap

    SortedOrderedListSetMapClass

  • 8/6/2019 5.2_CollectionFramework

    28/29

    28

    Key PointsKey Points

    Collections Framework contains:

    1. Interfaces

    2. Implementations

    3. Operations A list cares about the index.

    A set cares about uniqueness, it does not allow

    duplicates.

    A map cares about unique identifiers.

  • 8/6/2019 5.2_CollectionFramework

    29/29

    29

    Questions and CommentsQuestions and Comments