Lecture 7 Object Oriented Programming in Java
-
Upload
sylvester-evans -
Category
Documents
-
view
35 -
download
0
description
Transcript of Lecture 7 Object Oriented Programming in Java
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
1
Lecture 7Object Oriented Programming in Java
Advanced TopicsCollection Framework
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
2
Today’s Lecture
• Trail: Collections• Lessons:
– Introduction – Interfaces– Implementations– Algorithms
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
3
Collections
• Collections simply allow you to group together related objects
• Collections provide sophisticated ways to hold and even manipulate these many objects
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
7
Abstraction of a Framework
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
8
Collection Framework Architecture
• Interfaces• Abstract Implementations• General Purpose Implementations• Legacy Implementations
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
9
Collection Framework Interfaces
• Interfaces are the roles a component of object can play
• Here they specify the abstract data types which represent collections:
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
10
Collection
• Collection– A Collection represents a group of objects, known as
its elements
• Behaviors:– Basic Operations– Bulk Operations– Array Operations
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
11
Collection Methodspublic 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[]);}
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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);
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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}
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
15
Set Interface Methods
• Same as Collection Methods but the contract is different:– No duplicates are maintained
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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 <-> description
– address book• name <-> phone number
A
B
C
D
1
2
3
Illegal mapping
Map
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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();
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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( ).
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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();}
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
20
Implementations
HashTable
ResizableArray
BalancedTree
LinkedList
Set HashSet TreeSet
List ArrayList LinkedList
Map HashMap TreeMap
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
21
Roll-out your own
• Abstract Implementations– AbstractCollection– AbstractSet– AbstractList– AbstractMap
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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 < 7; i++) cats.add(new Cat(i)); Iterator e = cats.iterator(); while(e.hasNext()) ((Cat)e.next()).print(); }}
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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 < 7; i++) cats.add(new Cat(i)); Iterator e = cats.iterator(); while(e.hasNext()) ((Cat)e.next()).print(); }}
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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 < 7; i++) cats.add(new Cat(i)); Iterator e = cats.iterator(); while(e.hasNext()) ((Cat)e.next()).print(); }}
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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 < 7; i++) cats.add(new Cat(i)); Iterator e = cats.iterator(); while(e.hasNext()) ((Cat)e.next()).print(); }}
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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…. }
}
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
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);
}}
June 1, 2000 Object Oriented Programming in Java (95-707)Java Language Basics
29
Unknown Types
public class CatsAndDogs { public static void main(String[] args) {
ArrayList cats = new ArrayList();
for(int i = 0; i < 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 < cats.size(); i++) ((Cat)cats.get(i)).print(); // Dog is detected only at run-time }}