Object Oriented Programming in Java (95-707) Java Language Basics - PowerPoint PPT Presentation

About This Presentation
Title:

Object Oriented Programming in Java (95-707) Java Language Basics

Description:

Collections simply allow you to group together related objects. Collections provide sophisticated ways to hold and even manipulate these many objects ... – PowerPoint PPT presentation

Number of Views:227
Avg rating:3.0/5.0
Slides: 30
Provided by: alaink
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Programming in Java (95-707) Java Language Basics


1
Lecture 7Object Oriented Programming in Java
  • Advanced Topics
  • Collection Framework

2
Todays Lecture
  • Trail Collections
  • Lessons
  • Introduction
  • Interfaces
  • Implementations
  • Algorithms

3
Collections
  • Collections simply allow you to group together
    related objects
  • Collections provide sophisticated ways to hold
    and even manipulate these many objects

4
History
  • The Java 2 collection framework represents a
    thorough redesign of the rather poor showings in
    Java 1.0 and 1.1
  • simple arrays are efficient but difficult to use
    for complex tasks such copying, duplicating,
    sorting,...
  • Vector and Hashtable classes in JDK 1.x where
    useful but flawed in design and lacked standard
    built-in functionality
  • If you were familiar with the Vector and
    Hashtable classes you will still find them in
    Java 2. They still are maintained for backward
    compatibility but still suffer from some of the
    same problems

5
Purpose of an OO Framework
  • Reuse and programming- by- difference
  • Using inheritance and stub class implementations
    (abstract classes), a new class can be
    implemented by providing only what is different
    in this class compared to one which already
    exists
  • The effort to develop a new class is proportional
    to the difference in functionality between the
    particular class and that in the framework

6
Frameworks vs. Class Libraries
  • Framework and Class Libraries are similar but
    different
  • Class libraries have no predefined flow of
    control, no predefined interactions. There are
    just a set of instantiated classes by the client
  • Framework provide for customization by
    sub-classing, Provide default behaviors, Defines
    object interactions
  • The collection framework is a little bit of both
    (class library and true framework)
  • The Collection Framework is a good example of the
    power of object oriented design

7
Abstraction of a Framework
8
Collection Framework Architecture
  • Interfaces
  • Abstract Implementations
  • General Purpose Implementations
  • Legacy Implementations

9
Collection Framework Interfaces
  • Interfaces are the roles a component of object
    can play
  • Here they specify the abstract data types which
    represent collections

10
Collection
  • Collection
  • A Collection represents a group of objects, known
    as its elements
  • Behaviors
  • Basic Operations
  • Bulk Operations
  • Array Operations

11
Collection Methods
  • public interface Collection
  • // Basic Operations
  • int size()
  • boolean isEmpty()
  • boolean contains(Object element)
  • boolean add(Object element) // Optional
  • boolean remove(Object element) // Optional
  • Iterator iterator()
  • // Bulk Operations
  • boolean addAll(Collection c) // Optional
  • boolean removeAll(Collection c) // Optional
  • boolean retainAll(Collection c) // Optional
  • .
  • // Array Operations
  • Object toArray()
  • Object toArray(Object a)

12
Lists
  • A List is an ordered collection (sometimes called
    a sequence)
  • Lists can contain duplicate elements
  • Examples
  • List of first name in the class sorted by
    alphabetical order
  • Eric, Fred, Fred, Greg, John, John, John
  • List of cars sorted by origin
  • Ford, Chevrolet, Jeep, Nissan, Toyota, BMW, VW

13
List Interface Methods
  • Inherits from Collection
  • Some additions
  • void add(int index, Object element)
  • boolean addAll(int index, Collection c)
  • Object get(int index)
  • Object remove(int index)
  • Object set(int index, Object element)
  • int lastIndexOf(Object o)
  • int indexOf(Object o)

14
Sets
  • A Set is a collection that cannot contain
    duplicate elements
  • Examples
  • Set of cars
  • BMW, Ford, Jeep, Chevrolet, Nissan, Toyota, VW
  • Nationalities in the class
  • Chinese, American, Canadian, Indian
  • Course schedule for John
  • 95-707, 90-203, 95-405

15
Set Interface Methods
  • Same as Collection Methods but the contract is
    different
  • No duplicates are maintained

16
Map
  • A Map is an object that maps keys to values. Maps
    cannot contain duplicate keys.
  • Each key can map to at most one value
  • Examples
  • Think of a dictionary
  • word lt-gt description
  • address book
  • name lt-gt phone number

A B C D
1 2 3
Illegal mapping
Map
17
Map Interface Methods
  • Basics
  • Object put(Object key, Object value)
  • Object get(Object key)
  • Object remove(Object key)
  • int size()
  • ...
  • Bulk
  • void putAll(Map t)
  • void clear()
  • Collection Views
  • public Set keySet()
  • public Collection values()
  • public Set entrySet()

18
Iterator Interface
  • Similar to the old Enumeration interface of
    Vector and Hashtable
  • An Iterator is an object whose job is to move
    through a sequence of objects and select each
    object in that sequence without the client
    programmer knowing or caring about the underlying
    structure of that sequence
  • Here is what you can do with an Iterator
  • Ask a container to hand you an Iterator using a
    method called iterator( ). This Iterator will be
    ready to return the first element in the sequence
    on your first call to its next( ) method.
  • Get the next object in the sequence with next( ).
  • See if there are any more objects in the sequence
    with hasNext( ).
  • Remove the last element returned by the iterator
    with remove( ).

19
Iterator Interface
  • The interface definition
  • public interface Iterator
  • boolean hasNext()
  • Object next()
  • void remove() // Optional
  • Sample code
  • static void filter(Collection c)
  • for (Iterator i c.iterator() i.hasNext()
    )
  • if (!cond(i.next()))
  • i.remove()

20
Implementations
21
Roll-out your own
  • Abstract Implementations
  • AbstractCollection
  • AbstractSet
  • AbstractList
  • AbstractMap

22
Overall Taxonomy
23
Cats and Dogs - I
  • // Simple container with Iterator.
  • import java.util.
  • public class CatsAndDogs
  • public static void main(String args)
  • ArrayList cats new ArrayList()
  • for(int i 0 i lt 7 i)
  • cats.add(new Cat(i))
  • Iterator e cats.iterator()
  • while(e.hasNext())
  • ((Cat)e.next()).print()

24
Cats and Dogs - II
// Simple container with Iterator. import
java.util. public class CatsAndDogs public
static void main(String args) ArrayList
cats new ArrayList() for(int i 0 i lt 7
i) cats.add(new Cat(i)) Iterator e
cats.iterator() while(e.hasNext())
((Cat)e.next()).print()
25
Cats and Dogs - III
  • // Simple container with Iterator.
  • import java.util.
  • public class CatsAndDogs
  • public static void main(String args)
  • ArrayList cats new ArrayList()
  • for(int i 0 i lt 7 i)
  • cats.add(new Cat(i))
  • Iterator e cats.iterator()
  • while(e.hasNext())
  • ((Cat)e.next()).print()

26
Cats and Dogs - IV
  • // Simple container with Iterator.
  • import java.util.
  • public class CatsAndDogs
  • public static void main(String args)
  • ArrayList cats new ArrayList()
  • for(int i 0 i lt 7 i)
  • cats.add(new Cat(i))
  • Iterator e cats.iterator()
  • while(e.hasNext())
  • ((Cat)e.next()).print()

27
CollectionPrinter
  • import java.util.
  • public class CollectionPrinter
  • static Collection fill(Collection c)
  • // add elements to the collection containers
    here
  • return c
  • static Map fill(Map m) // add elements to the
    map here
  • return m
  • public static void main(String args)
  • // fill various collection containers here.

28
Danger with Collections Unknown Type
  • public class Cat
  • private int catNumber
  • Cat(int i) catNumber i
  • void print()
  • System.out.println("Cat " catNumber)

public class Dog private int dogNumber
Dog(int i) dogNumber i void print()
System.out.println(Dog " dogNumber)
29
Unknown Types
  • public class CatsAndDogs
  • public static void main(String args)
  • ArrayList cats new ArrayList()
  • for(int i 0 i lt 7 i)
  • cats.add(new Cat(i))
  • // Not a problem to add a dog to cats
  • cats.add(new Dog(7))
  • for(int i 0 i lt cats.size() i)
  • ((Cat)cats.get(i)).print()
  • // Dog is detected only at run-time
Write a Comment
User Comments (0)
About PowerShow.com