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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Java Programming: Advanced Topics 1 Collections and Utilities.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
CE203 - Application Programming Autumn 2013CE203 Part 41 Part 4.
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.
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++)
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
15-Jun-15 Lists in Java Part of the Collections Framework.
Professor Evan Korth (adapted from Sun’s collections documentation)
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
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.
The Java Collections Package C. DeJong Fall 2001.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Chapter 19 Java Data Structures
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
CS Collection and Input/Output Classes CS 3331 Fall 2009.
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.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities Chapter 4.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
(c) University of Washington14-1 CSC 143 Java Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Data structures and algorithms in the collection framework 1 Part 2.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Chapter 18 Java Collections Framework
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.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 Interfaces in Java’s Collection Framework Rick Mercer.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Set and Map IS 313, Skeletons  Useful coding patterns  Should understand how and why they work how to use  possible quiz material.
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.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
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.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
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
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Java Collections OOP tirgul No
Introduction to Collections
Introduction to Collections
Lecture 6: Collections.
Introduction to Collections
Part of the Collections Framework
Introduction to Collections
Introduction to Collections
Part of the Collections Framework
Presentation transcript:

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

History In the original version of the Java® Development Kit, JDK 1.0, developers were provided very few data structures. These were: – Vector – Stack: which extended Vector – Hashtable: very similar to our implementation of HashMap – Dictionary: an abstract class that defined the interface and some functionality for classes that map keys to values. Dictionary served as the base class for Hashtable. – Enumeration: was a simple version of our Iterator that allowed you to iterate over instances of Hashtable and Vector.

The Java® Collections Framework The Java® designers realized that this set of data structures was inadequate. They prototyped a new set of data structures as a separate toolkit late in JDK 1.1, and then made it a formal part of the JDK in version 1.2. This later, fuller, better designed set of classes is called the Java® Collections Framework. There is a good tutorial on its use at ction s/index.html.

Collections Interfaces, 1 Collection Set SortedSet List Iterator ListIterator Map SortedMap

Collections Interfaces, 2 Collection: is the most basic interface; it has the functionality of an unordered list, aka a multi-set, a set that doesnt not check for duplicates. Set: adds set semantics; that is, it won't allow duplicate members. List: adds list semantics; that is, a sense of order and position SortedSet: adds order to set semantics; our binary search tree that just consisted of keys without values could be implemented as a SortedSet

Collections Interfaces, 3 Map: the basic interface for data structures that map keys to values; it uses an inner class called an Entry SortedMap: a map of ordered keys with values; our binary search tree is a good example Iterator: our Iterator except that it is fail fast; it throws a ConcurrentModificationException if you use an instance after the underlying Collection has been modified. ListIterator: a bidirectional iterator.

Collection Implementations The Java® designers worked out the architecture of interfaces independently of any data structures. In a second stage they created a number of concrete implementations of the interfaces based on slightly more sophisticated versions of the data structures that we have been studying: – Resizable Array: similar to the technique used for our Stack implementation. – Linked List: uses a doubly, not singly, linked list. – Hash Table: very similar to our implementation except that it will grow the number of slots once the load factor passes a value that can be set in the constructor. – Balanced Tree: similar to our binary search tree implementation but based on the more sophisticated Red-Black tree that rebalances the tree after some operations.

Collection Implementations, 2 Implementations Hash Table Resizable Array Balanced Tree Linked List Inter- faces ListArray-ListLinked- List SetHashSet Ordered Set TreeSet MapHashMap Ordered Map TreeMap

Collection Implementations, 3 Notice the gaps. There is no list based on a hash table. Why? What about a set implementation based on a linked list? The goal of the Java® designers was to create a small set of implementations to get users started, not an exhaustive set. They expect that developers will extend the set of collection classes just as they expect that developers will add more stream classes. The most important part of the collection design was the architecture of interfaces. That's why they called it a framework.

Implementation Choices Be careful of implementation choices. Not all the implementations are going to be efficient at all their operations. For instance, an ArrayList will allow you to remove its first element by calling remove( 0 ). We already know this will be particularly inefficient. You can also iterate over a LinkedList, l, using the following code, but once again it will be very inefficient. Why? for ( inti = 0; i < l.size(); i++ ) { Object o = l.get( i ); // do what you need with o }

Collection Interface public interface Collection { // Query Operations int size(); boolean isEmpty(); boolean contains(Object o); boolean containsAll(Collection c); Iterator iterator(); Object[] toArray(); Object[] toArray(Object a[]); boolean equals(Object o); int hashCode();

Collection Interface, 2 // Modification Operations boolean add(Object o); boolean remove(Object o); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear(); }

Bulk Operations Note the bulk operations. All the interfaces derived from Collection support them: – boolean containsAll( Collection c ) – boolean addAll( Collection c ) – boolean removeAll( Collection c ) – boolean retainAll( Collection c ) containsAll() returns true if the target of the method contains all the elements in the argument collection. The other three methods return true if the operation changes the target collection.

Bulk Constructors All Collection (Map) implementations support at least two constructors. – a default constructor that creates an empty Collection (Map) of the appropriate type, and – a constructor that takes a Collection (Map) argument that creates a Collection (Map) of the appropriate type that contains references to all the objects in the Collection (Map) supplied as the argument.

Array Operations All Collection implementations also possess a method with the signature: Object [ ] toArray() which returns the contents of the collection in an appropriately sized array. A variant Object[ ] to Array (Object a[ ]); returns all the elements of the collection that match the type of the array, a[ ], in a[ ] itself or an appropriately sized array of the same type.

Array Examples Collection c = new HashSet(); // put things in c String[] s = (String[]) c.toArray(new String[0]); returns all the String elements of c in an array of Strings. Collection c = new HashSet(); // if c contains only Strings String[] s = (String[]) c.toArray( );

List Interface public interface List extends Collection { // adds the following to Collection boolean addAll(int index, Collection c); Object get(int index); Object set(int index, Object element); void add(int index, Object element); Object remove(int index); int indexOf(Object o); int lastIndexOf(Object o); ListIterator listIterator(); ListIterator listIterator(int index); List subList(int fromIndex, int toIndex); }

Views Several of the interfaces also support view operations. In a view operation, a collection returns another collection that provides a specialized and usually partial view of its contents. A good example is List subList( int from, int to ) This method returns a second list that starts with the from'th element of the parent list and ends just before the to'th element. But the returned sublist is not a copy. It remains part of the original list. As an example, an elegant way to delete the 4th through 10th elements of a list is the following: myList.subList( 3, 10 ).clear();

Set Interface public interface Set extends Collection { // has the same methods as Collection // but with stricter semantics }

SortedSet Interface public interface SortedSet extends Set { // adds the following to Set Comparator comparator(); SortedSet subSet(Object fromElement, Object toElement); SortedSet headSet(Object toElement); SortedSet tailSet(Object fromElement); Object first(); Object last(); }

Iterator Interface public interface Iterator { boolean hasNext(); Object next(); void remove(); }

ListIterator Interface public interface ListIterator extends Iterator { // adds the following to Iterator boolean hasPrevious(); Object previous(); int nextIndex(); int previousIndex(); void set(Object o); void add(Object o); }

Concurrent Modification Iterators have to keep references to the underlying data structure to maintain their sense of current position. Thought experiment: – create a LinkedList, add elements, get an iterator, advance it – now using a method in the list not the iterator, delete the iterator's next item – now call next() on the iterator – what goes wrong? Any time you create an iterator, modify the underlying collection, and then use the iterator, you will get a ConcurrentModificationException. How do you think they implement the check?

Concurrent Modification, Before

Concurrent Modification, After

Map Interface public interface Map { // Query Operations int size(); boolean isEmpty(); boolean containsKey(Object key); boolean containsValue(Object value); Object get(Object key);

Map Interface, 2 // Modification Operations Object put(Object key, Object value); Object remove(Object key); void putAll(Map t); void clear(); // Views public Set keySet(); public Collection values(); public Set entrySet(); boolean equals(Object o); int hashCode();

Nested Map.Entry Interface public interface Entry { Object getKey(); Object getValue(); Object setValue(Object value); boolean equals(Object o); int hashCode(); }

Map Views Maps also provide views. In fact they are crucial because a Map does not provide an iterator() method. They are two separate idioms for iterating over a Map, depending on whether you want to iterate over the keys or the values: Map m =...; // iterates over keys for (Iterator i=m.keySet().iterator(); i.hasNext();){...} // iterates over values for (Iterator i=m.values().iterator(); i.hasNext();) {...} In the second example, values() returns a Collection, not a Set, because the same value can occur multiple times in a map while a key must be unique.

SortedMap Interface public interface SortedMap extends Map { // adds the following to Map Comparator comparator(); SortedMap subMap(Object fromKey, Object toKey); SortedMap headMap(Object toKey); SortedMap tailMap(Object fromKey); Object firstKey(); Object lastKey(); }

Optional Operations Not all implementations implement all the operations in an interface. The documentation makes it clear whether a particular implementation implements a given method. If it doesn't, it throws an UnsupportedOperationException (unchecked). At first, this just seems perverse, but the goal is to provide more flexibility. Imagine a program where you want to maintain a master index in which you want a normal user to be able to look up entries but only a privileged user to add entries to the index. The index could be implemented as a SortedMap, but then how could you keep an arbitrary user from adding and deleting entries? The answer is to subclass SortedMap and override each method you wanted to forbid with a simple method that throws an UnsupportedOperationException.

Algorithms The Collections class contains implementations of a number of useful algorithms implemented as static methods that take a List or sometimes a more general Collection as the target of the algorithm. Algorithms include sort(), reverse(), min(), max(), fill(Object o), etc. static void sort( List l, Comparator c ) static void fill( List l, Object o ) There is a similar class Arrays, which implements most of the same algorithms for arrays.

Read-Only Collections Collections can also produce unmodifiable (read only) views of all types of collections that will throw an UnsupportedOperationException on any write operation: public static Collection unmodifiableCollection(Collection c); public static Set unmodifiableSet(Set s);...