0% found this document useful (0 votes)
43 views29 pages

Object Oriented Programming in Java: Advanced Topics Collection Framework

This document provides an overview of the Java Collection Framework. It introduces common collection interfaces like Collection, List, Set, and Map. It describes the purpose of the framework in enabling code reuse. Key classes that implement the interfaces, like ArrayList and HashSet, are also mentioned. Finally, the Iterator interface is summarized as a way to traverse the elements of a collection.

Uploaded by

Alfonso Rivero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views29 pages

Object Oriented Programming in Java: Advanced Topics Collection Framework

This document provides an overview of the Java Collection Framework. It introduces common collection interfaces like Collection, List, Set, and Map. It describes the purpose of the framework in enabling code reuse. Key classes that implement the interfaces, like ArrayList and HashSet, are also mentioned. Finally, the Iterator interface is summarized as a way to traverse the elements of a collection.

Uploaded by

Alfonso Rivero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Lecture 7

Object Oriented Programming in Java

Advanced Topics
Collection Framework

June 1, 2000 Object Oriented Programming in Java (95-707) 1


Java Language Basics
Today’s Lecture

• Trail: Collections
• Lessons:
– Introduction
– Interfaces
– Implementations
– Algorithms

June 1, 2000 Object Oriented Programming in Java (95-707) 2


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

June 1, 2000 Object Oriented Programming in Java (95-707) 3


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
June 1, 2000 Object Oriented Programming in Java (95-707) 4
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

June 1, 2000 Object Oriented Programming in Java (95-707) 5


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

June 1, 2000 Object Oriented Programming in Java (95-707) 6


Java Language Basics
Abstraction of a Framework

June 1, 2000 Object Oriented Programming in Java (95-707) 7


Java Language Basics
Collection Framework Architecture

• Interfaces
• Abstract Implementations
• General Purpose Implementations
• Legacy Implementations

June 1, 2000 Object Oriented Programming in Java (95-707) 8


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:

June 1, 2000 Object Oriented Programming in Java (95-707) 9


Java Language Basics
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) 10


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[]);
}

June 1, 2000 Object Oriented Programming in Java (95-707) 11


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

June 1, 2000 Object Oriented Programming in Java (95-707) 12


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);

June 1, 2000 Object Oriented Programming in Java (95-707) 13


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}

June 1, 2000 Object Oriented Programming in Java (95-707) 14


Java Language Basics
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) 15


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 A 1
– address book
B 2
• name <-> phone number
C 3
D Illegal mapping

Map
June 1, 2000 Object Oriented Programming in Java (95-707) 16
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();

June 1, 2000 Object Oriented Programming in Java (95-707) 17


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( ).

June 1, 2000 Object Oriented Programming in Java (95-707) 18


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();
}

June 1, 2000 Object Oriented Programming in Java (95-707) 19


Java Language Basics
Implementations

Hash Resizable Balanced Linked


Table Array Tree List

Set HashSet TreeSet

List ArrayList LinkedList

Map HashMap TreeMap

June 1, 2000 Object Oriented Programming in Java (95-707) 20


Java Language Basics
Roll-out your own

• Abstract Implementations
– AbstractCollection
– AbstractSet
– AbstractList
– AbstractMap

June 1, 2000 Object Oriented Programming in Java (95-707) 21


Java Language Basics
Overall Taxonomy

June 1, 2000 Object Oriented Programming in Java (95-707) 22


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();
}
}

June 1, 2000 Object Oriented Programming in Java (95-707) 23


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();
}
}

June 1, 2000 Object Oriented Programming in Java (95-707) 24


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();
}
}
June 1, 2000 Object Oriented Programming in Java (95-707) 25
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();
}
}
June 1, 2000 Object Oriented Programming in Java (95-707) 26
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….
}
}

June 1, 2000 Object Oriented Programming in Java (95-707) 27


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);

}}

June 1, 2000 Object Oriented Programming in Java (95-707) 28


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
}
}

June 1, 2000 Object Oriented Programming in Java (95-707) 29


Java Language Basics

You might also like