1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

Slides:



Advertisements
Similar presentations
Introduction to Computation and Problem Solving Class 32: The Java® Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Processing Data in Collections Chapter Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Frameworks and Collections Horstmann ch QUIZ What is the difference between a program library and a framework? 1.There is no difference 2.When.
Working With Collections in the AP ™ Java Subset 2003 ACTE Convention © 2003 by Kenneth A. Lambert.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
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.
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.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java 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.
FrameworksFrameworks chap8. Framework Set of cooperating classes Structures the essential mechanisms of a problem domain Framework != design pattern Typical.
05 - Containers DRAFT COPY © S. Uchitel, 2004 Container OrderedDuplicates BagsNOYES SetsNONO ListsYESYES MapsNO.
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.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
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.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Lists and the Collection Interface Review inheritance & collections.
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
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Java 2 Collections Bartosz Walter Software Engineering II.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
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.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
1/20/05A-1 © 2001 T. Horton CS 494 Adv. SW Design and Development A Tasting…. Course 1: Design patterns: Intro, example Course 2: Inheritance, Interfaces,
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.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Linked Lists Chapter 5 (continued)
Data Structures Lakshmish Ramaswamy.
Programming in Java Lecture 11: ArrayList
Collections in Java The objectives of this lecture are:
Collections Framework
Introduction to Collections
Linked Lists Chapter 5 (continued)
Part of the Collections Framework
Linked Lists Chapter 5 (continued)
Java Generics & Iterators
Presentation transcript:

1 Frameworks Part 2

2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding new collection classes that can interact with existing classes

3 Overview of collections framework Collection: data structure that contains multiple objects (elements) Collections framework specifies four interfaces: –Collection: general collection (bag) –Set: unordered collection of unique elements –SortedSet: ordered collection of unique elements –List: ordered collection of elements; can contain duplicates

4 Concrete classes supplied by collections framework HashSet: a set implementation that uses hashing to locate the set elements TreeSet: a sorted set implementation that stores the elements in a balanced binary tree LinkedList and ArrayList: two implementations of the List interface type

5 Collections Framework

6 Collection and Iterator Collection and Iterator are the fundamental interfaces of the collections framework A collection is any class that can store multiple elements; individual collection classes enforce different rules for how data are to be stored and located An iterator is a mechanism for visiting all elements in a collection

7 Methods specified by Collection interface boolean add(Object obj) boolean addAll(Collection c) void clear() boolean contains(Object obj) boolean containsAll(Collection c) boolean equals(Object obj) int hashCode() boolean isEmpty() Iterator iterator() boolean remove(Object obj) boolean removeAll(Collection c) boolean retainAll(Collection c) int size() Object[] toArray() Object[] toArray(Object[] a

8 AbstractCollection class Collection is a hefty interface: client programmer must implement 15 methods AbstractCollection class relieves client programmer of this burden; provides reasonable default implementations for almost all of these methods

9 Example method from AbstractCollection public Object[] toArray() { Object[] result = new Object[size()]; Iterator e = iterator(); for (int i=0; e.hasNext(); i++) result[i] = e.next(); return result; } Note use of size() and iterator(); these are the two methods left undefined by the abstract class. Which pattern is in play here?

10 Notes on AbstractCollection Methods like toArray() implement the template method pattern; since they are implementations, not just specifications, they must appear in a class Methods size() and iterator() are left undefined by AbstractCollection; most concrete collections derived from this class also override add() and remove()

11 Adding new class to Collections framework Text uses example of Queue class; originally developed in chapter 3 without connection to framework Why use framework? –Framework provides a number of methods that work on all Collections; addAll(), for example, does bulk addition of all elements from one collection to another –If Queue is part of framework, such methods are automatically applicable to the new class

12 Set Interface Definition of Set: public interface Set extends Collection{} No methods - why have it? –Set is conceptually a subtype of Collection; Set is unordered Collection without duplicates –Separate interface serves to tag objects with Set quality; algorithms can require Set objects as distinct from other Collections

13 List interface List: ordered collection of elements Each list position accessible via integer index Interface specifies several additional methods to general Collection type; most of these are concerned with index positions

14 List interface methods boolean add(int index, Object obj) boolean addAll(int index, Collection c) Object get(int index) int indexOf(Object obj) int lastIndexOf(Object obj) ListIterator listIterator() ListIterator listIterator(int index) Object remove(int index) Object set(int index, int Object) List subList(int fromIndex, int toIndex)

15 ListIterator interface Subtype of Iterator Adds: –support for indexing –ability to move both forward and backward through list

16 ListIterator interface Methods include: –int nextIndex() –int previousIndex() –boolean hasPrevious() –Object previous() –void add(Object obj) –void set(Object obj)

17 List classes ArrayList LinkedList –provides “indexed” access to individual list elements, but it’s klunky and slow (must visit all predecessor elements to get to desired element) –highlights weakness in the framework design; should have provided separate interface types for indexed collections (arrays) and ordered collections (lists)

18 RandomAccess interface Version 1.4 of Java sdk has RandomAccess interface to fix design problem Tagging interface (no methods) - can use to test whether or not indexed access is really appropriate for a particular object ArrayList implements RandomAccess, but LinkedList does not

19 Optional operations API documentation tags some methods as optional operations Default implementations throw UnsupportedOperationException Collection methods add() and remove() are examples

20 Views View: object that implements an interface of collections framework, but permits only restricted access to data structure Optional operations exist to support views Built-in Java type array has no methods; can apply view to enhance array functionality

21 Views Example: asList method –asList turns an array into a view object: collection that implements List interface, so List methods can be applied to array elements –does not copy array elements; get and set methods of view object access original array –Can’t apply add() or remove() methods to view, because size of underlying array can’t be changed: this is why these methods are “optional”

22 Graph editor framework Problem domain: interactive editing of graphs Graph: collection of nodes and edges arranged in certain shape Examples of graphs: –class relationship diagrams –electronic circuit diagrams –flow charts

23 Graph editor framework Encapsulates aspects common to all graph editing applications –user interface –event handling Application programmer extends graph editor framework, defines specific behavior for nodes and edges

24 User interface Toolbar includes: grabber tool for selecting elements buttons for each node/edge type menus: loading/saving diagram deleting selected elements Drawing area: Mouse used for drawing; behavior depends on current tool: if node, clicking empty space inserts new node if grabber, clicking on element selects it; dragging operation moves selected node & connected edges if edge, drag from one node to another inserts edge

25 Division of responsibility Drawing shapes of nodes & edges: application Hit testing (element hit by mouse click): application Drawing toolbar: framework Mouse listening: framework

26 Division of responsibility Application programmer must inform framework about node & edge types of particular graph type –concrete graph gives framework prototype objects –toolbar queries graph for prototypes, adds buttons for each one –nodes & edges draw themselves in paintIcon method of button icon object –when user inserts new node or edge, object corresponding to selected tool is cloned & added to graph

27 Prototype pattern Teaches how a system can instantiate classes that are not known when system is built Nature of node & edge types unknown when framework code is designed - use prototype pattern to solve this problem

28 Prototype pattern Context: –A system instantiates objects of classes that are not known when the system is built. –You do not want to require a separate class for each kind of object. –You want to avoid a separate hierarchy of classes whose responsibility it is to create the objects.

29 Prototype pattern Solution: –Define a prototype interface type that is common to all created objects. –Supply a prototype object for each kind of object that the system creates. –Clone the prototype object whenever a new object of the given kind is required.

30 Prototype pattern

31 Prototype pattern applied to graph editor Name in pattern: –Prototype –ConcretePrototype1 –Creator Name in graph editor: –Node –CircleNode –Instance of GraphPanel that handles mouse operation for adding new nodes

32 Framework classes Node and Edge interfaces include methods: –draw(): used in painting graph –contains(): tests whether mouse click falls within range of an element –getBounds(): returns rectangle enclosing element –clone(): clones prototypes when inserting new elements

33 Framework classes Edge interface includes methods getStart() and getEnd() which return nodes connected by edge Node interface method getConnectionPoint() returns optimal attachment point for edge Edge method getConnectionPoints() yields 2 end points of edge; used by grabbers to mark currently selected edge Framework also supplies AbstractEdge class for programmer convenience

34 Framework classes Graph class: –collects nodes and edges –has methods for adding, removing, finding and drawing nodes and edges –is abstract; subclasses must override methods that populate the toolbar: public abstract Node[] getNodePrototypes() public abstrac Edge[] getEdgePrototypes

35 Framework classes GraphFrame class manages the toolbar, menu bar, and graph panel Toolbar class holds toggle buttons for node and edge icons GraphPanel class displays graph, handles mouse events for editing commands

36 Building Graph Editor Application For each node and edge type, define class that implements Node or Edge Define subclass of Graph with methods getNodePrototypes and getEdgePrototypes defined to supply prototype objects for graph elements Supply a main() method