290 likes | 377 Views
Lecture 7 Object Oriented Programming in Java. Advanced Topics Collection Framework. Today’s Lecture. Trail: Collections Lessons: Introduction Interfaces Implementations Algorithms. Collections. Collections simply allow you to group together related objects
E N D
Lecture 7Object Oriented Programming in Java Advanced Topics Collection Framework Object Oriented Programming in Java (95-707)Java Language Basics
Today’s Lecture • Trail: Collections • Lessons: • Introduction • Interfaces • Implementations • Algorithms Object Oriented Programming in Java (95-707)Java Language Basics
Collections • Collections simply allow you to group together related objects • Collections provide sophisticated ways to hold and even manipulate these many objects Object Oriented Programming in Java (95-707)Java Language Basics
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 Object Oriented Programming in Java (95-707)Java Language Basics
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 Object Oriented Programming in Java (95-707)Java Language Basics
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 Object Oriented Programming in Java (95-707)Java Language Basics
Abstraction of a Framework Object Oriented Programming in Java (95-707)Java Language Basics
Collection Framework Architecture • Interfaces • Abstract Implementations • General Purpose Implementations • Legacy Implementations Object Oriented Programming in Java (95-707)Java Language Basics
Collection Framework Interfaces • Interfaces are the roles a component of object can play • Here they specify the abstract data types which represent collections: Object Oriented Programming in Java (95-707)Java Language Basics
Collection • Collection • A Collection represents a group of objects, known as its elements • Behaviors: • Basic Operations • Bulk Operations • Array Operations Object Oriented Programming in Java (95-707)Java Language Basics
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[]); } Object Oriented Programming in Java (95-707)Java Language Basics
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 Object Oriented Programming in Java (95-707)Java Language Basics
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); Object Oriented Programming in Java (95-707)Java Language Basics
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} Object Oriented Programming in Java (95-707)Java Language Basics
Set Interface Methods • Same as Collection Methods but the contract is different: • No duplicates are maintained Object Oriented Programming in Java (95-707)Java Language Basics
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 Object Oriented Programming in Java (95-707)Java Language Basics
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(); Object Oriented Programming in Java (95-707)Java Language Basics
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( ). Object Oriented Programming in Java (95-707)Java Language Basics
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(); } Object Oriented Programming in Java (95-707)Java Language Basics
Implementations Object Oriented Programming in Java (95-707)Java Language Basics
Roll-out your own • Abstract Implementations • AbstractCollection • AbstractSet • AbstractList • AbstractMap Object Oriented Programming in Java (95-707)Java Language Basics
Overall Taxonomy Object Oriented Programming in Java (95-707)Java Language Basics
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(); } } Object Oriented Programming in Java (95-707)Java Language Basics
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(); } } Object Oriented Programming in Java (95-707)Java Language Basics
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(); } } Object Oriented Programming in Java (95-707)Java Language Basics
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(); } } Object Oriented Programming in Java (95-707)Java Language Basics
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…. } } Object Oriented Programming in Java (95-707)Java Language Basics
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); }} Object Oriented Programming in Java (95-707)Java Language Basics
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 } } Object Oriented Programming in Java (95-707)Java Language Basics