Download presentation
Presentation is loading. Please wait.
Published byEustacia McKinney Modified over 9 years ago
1
Copyright (c) Systems and Computer Engineering, Carleton University 2001 1 94.204* Object-Oriented Software Development Unit 13 The Collections Framework revised February 25, 2001
2
Copyright (c) Systems and Computer Engineering, Carleton University 2001 2 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.
3
Copyright (c) Systems and Computer Engineering, Carleton University 2001 3 The Collections framework In 94.202 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 …
4
Copyright (c) Systems and Computer Engineering, Carleton University 2001 4 The Collections Framework Below is a partial view of the Collection Framework’s interface hierarchy: Collection SetList Map
5
Copyright (c) Systems and Computer Engineering, Carleton University 2001 5 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(…) !
6
Copyright (c) Systems and Computer Engineering, Carleton University 2001 6 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!
7
Copyright (c) Systems and Computer Engineering, Carleton University 2001 7 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.
8
Copyright (c) Systems and Computer Engineering, Carleton University 2001 8 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…
9
Copyright (c) Systems and Computer Engineering, Carleton University 2001 9 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
10
Copyright (c) Systems and Computer Engineering, Carleton University 2001 10 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
11
Copyright (c) Systems and Computer Engineering, Carleton University 2001 11 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.