Copyright (c) Systems and Computer Engineering, Carleton University 2001 1 94.204* Object-Oriented Software Development Unit 13 The Collections Framework.

Slides:



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

5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
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.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.
15-Jun-15 Lists in Java Part of the Collections Framework.
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.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Java's Collection Framework
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.
Java Collections Framework A presentation by Eric Fabricant.
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.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
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.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
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
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
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.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
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
C19: Collection Classes (don’t forget to look at all the online code examples)
Hashing By Emily Nelson. The Official Definition Using a hash function to turn some kind of data in relatively small integers or Strings The “hash code”
CS-2852 Data Structures Week 4, Class 1 - Review Review! Thursday Exam I - Review Implementing ArrayList Big-O Iterators – High-level description Linked.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
Introduction to Collections
Introduction to Collections
Programming in Java Lecture 11: ArrayList
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
Hashing in java.util
Web Design & Development Lecture 6
Presentation transcript:

Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework revised February 25, 2001

Copyright (c) Systems and Computer Engineering, Carleton University Reuse with Frameworks We have seen how OO promotes code reuse with inheritance and delegation “A framework is a set of classes that form the basis for building advanced functionality.” (Core Java vol.2) “The user of a framework forms subclasses to extend the functionality without having to reinvent the basic mechanisms.” for example, Swing is a framework for user interfaces.

Copyright (c) Systems and Computer Engineering, Carleton University The Collections framework In you learnt how to implement container classes such as bags, linked lists, stacks… Such collections are very useful in everyday programming, and it would be tedious and error prone to have to implement them from scratch for each new application. We already know that Java provides Arrays, Vectors and Hashtables, but clearly they are not enough! Good news: since JDK1.2, there is a whole Collection Framework available! –The designers of the Collections Framework decided to use Java interfaces …

Copyright (c) Systems and Computer Engineering, Carleton University The Collections Framework Below is a partial view of the Collection Framework’s interface hierarchy: Collection SetList Map

Copyright (c) Systems and Computer Engineering, Carleton University The Collections Framework The Collection interface defines generic methods such as: int size(); boolean add(Object o); boolean addAll(Collection other); boolean remove(Object o); boolean removeAll(Collection other); boolean contains(Object o);... (There’s more!) Notice – the return values – the “generic” argument types –no get(…) !

Copyright (c) Systems and Computer Engineering, Carleton University The Collections Framework Set is-a Collection where the order of elements does not matter, and where element duplicates are not accepted: –Set simply extends Collection and does not add any new methods. Example : Using a Set... boolean ret; ret = mySet.add(a); //first time true ret = mySet.add(a); //2nd time false! // b.equals(a) is true ret = mySet.add(b); //false as well!

Copyright (c) Systems and Computer Engineering, Carleton University The Collections Framework List is-a Collection where order matters and duplicates are accepted ( i.e. add(obj) should never return false here!). –List therefore extends Collection with methods for random (i.e. indexed) access: void add(int index, Object o); //void! Object get(int index); Object set(int index, Object o); Object remove(int index);... Map is an interface for collections that hold key/value pairs (can you name one?): boolean put(Object key, Object value); Object get(Object key);... Note that Map does not extend Collection.

Copyright (c) Systems and Computer Engineering, Carleton University The Collections Framework The Collection interface defines 15 methods… Implementing all methods can prove tedious, especially since some of them do not depend on any particular concrete collection, and could be implemented in terms of the others. For example: public boolean isEmpty() { if (size() == 0) return true; else return false; } But Java interfaces can’t provide implementation! So…

Copyright (c) Systems and Computer Engineering, Carleton University The Collections Framework The Collections Framework provides Abstract Classes which supply many of the routine implementations: Collection Set List Abstract Collection Abstract Set Abstract List So a developer can extend one of the abstract classes instead of implementing the corresponding interface! Map Abstract Map

Copyright (c) Systems and Computer Engineering, Carleton University The Collections Framework Example: ArrayList and HashMap extend AbstractList and AbstractMap respectively. ArrayList is very similar to Vector, and HashMap to Hashtable Browse JBuilder or the JDK Javadoc documentation to find the differences! AbstractListAbstractMap ArrayListHashMap

Copyright (c) Systems and Computer Engineering, Carleton University The Collections Framework Now where do the Vector and Hashtable classes fit in this framework? Vector and Hashtable existed before the collections framework was introduced. Vector now extends AbstractList. Since Hashtable was already a subclass of Dictionary, it could not also extend AbstractMap, so instead it implements Map directly: List Abstract List Vector Array List Map HashMap Abstract Map Hashtable Dictionary