June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

CSC 205 – Java Programming II Lecture 25 March 8, 2002.
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
15-Jun-15 Lists in Java Part of the Collections Framework.
Professor Evan Korth (adapted from Sun’s collections documentation)
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.
The Java Collections Package C. DeJong Fall 2001.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
11: Holding Your Objects Introduction to containers Container disadvantage: unknown type Iterators Container taxonomy Collection functionality List functionality.
Advanced Java Session 3 New York University School of Continuing and Professional Studies.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Data structures Abstract data types Java classes for Data structures and ADTs.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
The Java Collections Framework Based on
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Java 2 Collections Bartosz Walter Software Engineering II.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Collections. 2 Concept A collection is a data structure – actually, an object – to hold other objects, which let you store and organize objects in useful.
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
Collections Dwight Deugo Nesa Matic
Object Oriented Programming in Java Habib Rostami Lecture 7.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Java Collections CHAPTER 3-February-2003
Java Collections OOP tirgul No
Programming & Data Structures
Introduction to Collections
Introduction to Collections
Programming in Java Lecture 11: ArrayList
Lecture 6: Collections.
Introduction to Collections
Introduction to Collections
Part of the Collections Framework
Collections in Java The objectives of this lecture are:
Introduction to Collections
Collections Framework
Introduction to Collections
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Part of the Collections Framework
Java Generics & Iterators
Presentation transcript:

June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection 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 –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 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) 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, , }

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

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 22 Overall Taxonomy

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 }