13 Collections Framework - PowerPoint PPT Presentation

About This Presentation
Title:

13 Collections Framework

Description:

defines functionality for a map where its keys are sorted. SortedMap ... A set cares about uniqueness, it does not allow duplicates. ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 19
Provided by: ltn
Category:

less

Transcript and Presenter's Notes

Title: 13 Collections Framework


1
13 Collections Framework
2
Contents
  • What is Collection?
  • Collections Framework
  • Collections Hierarchy
  • Collections Implementations
  • Set
  • List
  • Map

3
Objectives
  • Define a collection
  • Describe the collections framework
  • Describe the collections hierarchy
  • Demonstrate each collection implementation

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

5
Collections Framework
  • Collections Framework is a unified architecture
    for managing collections
  • Main Parts of Collections Framework
  • Interfaces
  • Core interfaces defining common functionality
    exhibited by collections
  • Implementations
  • Concrete classes of the core interfaces providing
    data structures
  • Operations
  • Methods that perform various operations on
    collections

6
Collections FrameworkInterfaces
Core Interface Description
Collection specifies contract that all collections should implement
Set defines functionality for a set of unique elements
SortedSet defines functionality for a set where elements are sorted
List defines functionality for an ordered list of non- unique elements
Map defines functionality for mapping of unique keys to values
SortedMap defines functionality for a map where its keys are sorted
7
Collections FrameworkImplementations
Set List Map
HashSet ArrayList HashMap
LinkedHashSet LinkedList LinkedHashMap
TreeSet Vector Hashtable
TreeMap
Note Hashtable uses a lower-case t
8
Collections FrameworkOperations
  • 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 each collection type

9
Collections 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

10
Iterator
  • 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 ltvariablegt ltCollectionObjectgt.iterator(
    )

11
Collections HierarchySet and List
12
Collections HierarchyMap
13
Collection Implementations
List Lists of things (classes that implement
List)
Set Unique things (classes that implement Set)
Map Things with a unique ID (classes that
implement Map)
Next!
14
List
  • A List cares about the index.

LinkedList
Vector
ArrayList
15
List ImplementationsArrayList
  • import java.util.ArrayList
  • public class MyArrayList
  • public static void main(String args )
  • ArrayList alist new ArrayList( )
  • 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))

One Two Three
Back!
16
List ImplementationsVector
  • import java.util.Vector
  • public class MyVector
  • public static void main(String args )
  • Vector vecky new Vector( )
  • vecky.add(new Integer(1))
  • vecky.add(new Integer(2))
  • vecky.add(new Integer(3))
  • for(int x0 xlt3 x)
  • System.out.println(vecky.get(x))

1 2 3
Back!
17
List ImplementationsLinkedList
  • import java.util.LinkedList
  • public class MyLinkedList
  • public static void main(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 x0 xlt3 x)
  • System.out.println(arrayx)

1.0 2.0 3.0
Back!
18
Set
John
Luke
Paul
Mark
Fred
Peter
  • A Set cares about uniqueness, it doesnt allow
    duplicates.

TreeSet
LinkedHashSet
HashSet
19
Set ImplementationsHashSet
  • import java.util.
  • public class MyHashSet
  • public static void main(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( ))

d a c b
Back!
20
Set ImplementationsLinkedHashSet
  • 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 x0 xlt3 x)
  • System.out.println(arrayx)

One Two Three
Back!
21
Set ImplementationsTreeSet
  • import java.util.TreeSet
  • import java.util.Iterator
  • public class MyTreeSet
  • public static void main(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( ))

Jody Philippe Reggie Remiel
Back!
22
Map
ul
key value
Pl
Ma
Jn
Le
Mark
John
Paul
Luke
Paul
  • A Map cares about unique identifiers.

LinkedHashMap
TreeMap
Hashtable
HashMap
23
Map ImplementationsHashMap
  • import java.util.HashMap
  • public class MyHashMap
  • public static void main(String args )
  • HashMap map new HashMap( )
  • map.put("name", "Jody")
  • map.put("id", new Integer(446))
  • map.put("address", "Manila")
  • System.out.println("Name " map.get("name"))
  • System.out.println("ID " map.get("id"))
  • System.out.println("Address "
    map.get("address"))

Name Jody ID 446 Address Manila
Back!
24
Map ImplementationsHashtable
  • import java.util.Hashtable
  • public class MyHashtable
  • public static void main(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)

Table of Contents addressManila, nameJody,
id1001
Back!
25
Map ImplementationsLinkedHashMap
Jody 446 Manila Savings
  • import java.util.
  • public class MyLinkedHashMap
  • public static void main(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!
26
Map ImplementationsTreeMap
  • import java.util.
  • public class MyTreeMap
  • public static void main(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( ))

Printing the VALUES.... Manila 446 Jody
Back!
27
Collection Classes Summary
28
Key Points
  • Collections Framework contains
  • Interfaces
  • Implementations
  • Operations
  • A list cares about the index.
  • A set cares about uniqueness, it does not allow
    duplicates.
  • A map cares about unique identifiers.
Write a Comment
User Comments (0)
About PowerShow.com