3-February-2003cse403-10-Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering

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.
Collections. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level data structures: e.g.
Collections CS3250. Sources  Slides by Professor Chuck Allison  Core Java, by Cay S. Horstmann and Gary Cornell  The Java Tutorial 
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
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.
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.
The Java Collections Package C. DeJong Fall 2001.
Chapter 19 Java Data Structures
Java's Collection Framework
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.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
Advanced Java Session 3 New York University School of Continuing and Professional Studies.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 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.
111 © 2002, Cisco Systems, Inc. All rights reserved.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Data structures and algorithms in the collection framework 1.
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.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
The Java Collections Framework Based on
Java 2 Collections Bartosz Walter Software Engineering II.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
16-August-2002cse Libraries © 2002 University of Washington1 Class Libraries CSE 142, Summer 2002 Computer Programming 1
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.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
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.
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.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
University of Limerick1 Collections The Collection Framework.
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 CHAPTER 3-February-2003
Java Collections OOP tirgul No
Fundamental of Java Programming
Programming & Data Structures
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
Part of the Collections Framework
Presentation transcript:

3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering

3-February-2003cse Collections © 2003 University of Washington2 Readings and References References »"Collections", Java tutorial »

3-February-2003cse Collections © 2003 University of Washington3 Java 2 Collections A collection is an object that groups multiple elements into a single unit Very useful »store, retrieve and manipulate data »transmit data from one method to another »data structures and methods written by hotshots in the field Joshua Bloch, who also wrote the Collections tutorial

3-February-2003cse Collections © 2003 University of Washington4 Collections Framework Unified architecture for representing and manipulating collections. A collections framework contains three things »Interfaces »Implementations »Algorithms

3-February-2003cse Collections © 2003 University of Washington5 Collections Framework Diagram Interfaces, Implementations, and Algorithms From Thinking in Java, page 462

3-February-2003cse Collections © 2003 University of Washington6 Collection Interface Defines fundamental methods »int size(); »boolean isEmpty(); »boolean contains(Object element); »boolean add(Object element); // Optional »boolean remove(Object element); // Optional »Iterator iterator(); These methods are enough to define the basic behavior of a collection Provides an Iterator to step through the elements in the Collection

3-February-2003cse Collections © 2003 University of Washington7 Iterator Interface Defines three fundamental methods »Object next() »boolean hasNext() »void remove() These three methods provide access to the contents of the collection An Iterator knows position within collection Each call to next() “reads” an element from the collection »Then you can use it or remove it

3-February-2003cse Collections © 2003 University of Washington8 Iterator Position

3-February-2003cse Collections © 2003 University of Washington9 Example - SimpleCollection public class SimpleCollection { public static void main(String[] args) { Collection c; c = new ArrayList(); System.out.println(c.getClass().getName()); for (int i=1; i <= 10; i++) { c.add(i + " * " + i + " = "+i*i); } Iterator iter = c.iterator(); while (iter.hasNext()) System.out.println(iter.next()); }

3-February-2003cse Collections © 2003 University of Washington10 List Interface Context Collection List

3-February-2003cse Collections © 2003 University of Washington11 List Interface The List interface adds the notion of order to a collection The user of a list has control over where an element is added in the collection Lists typically allow duplicate elements Provides a ListIterator to step through the elements in the list.

3-February-2003cse Collections © 2003 University of Washington12 ListIterator Interface Extends the Iterator interface Defines three fundamental methods »void add(Object o) - before current position »boolean hasPrevious() »Object previous() The addition of these three methods defines the basic behavior of an ordered list A ListIterator knows position within list

3-February-2003cse Collections © 2003 University of Washington13 Iterator Position - next(), previous()

3-February-2003cse Collections © 2003 University of Washington14 ArrayList and LinkedList Context ArrayListLinkedList Collection List

3-February-2003cse Collections © 2003 University of Washington15 List Implementations ArrayList »low cost random access »high cost insert and delete »array that resizes if need be LinkedList »sequential access »low cost insert and delete »high cost random access

3-February-2003cse Collections © 2003 University of Washington16 ArrayList overview Constant time positional access (it’s an array) One tuning parameter, the initial capacity public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException( "Illegal Capacity: "+initialCapacity); this.elementData = new Object[initialCapacity]; }

3-February-2003cse Collections © 2003 University of Washington17 ArrayList methods The indexed get and set methods of the List interface are appropriate to use since ArrayLists are backed by an array »Object get(int index) »Object set(int index, Object element) Indexed add and remove are provided, but can be costly if used frequently »void add(int index, Object element) »Object remove(int index) May want to resize in one shot if adding many elements »void ensureCapacity(int minCapacity)

3-February-2003cse Collections © 2003 University of Washington18 LinkedList overview Stores each element in a node Each node stores a link to the next and previous nodes Insertion and removal are inexpensive »just update the links in the surrounding nodes Linear traversal is inexpensive Random access is expensive »Start from beginning or end and traverse each node while counting

3-February-2003cse Collections © 2003 University of Washington19 LinkedList entries private static class Entry { Object element; Entry next; Entry previous; Entry(Object element, Entry next, Entry previous) { this.element = element; this.next = next; this.previous = previous; } private Entry header = new Entry(null, null, null); public LinkedList() { header.next = header.previous = header; }

3-February-2003cse Collections © 2003 University of Washington20 LinkedList methods The list is sequential, so access it that way »ListIterator listIterator() ListIterator knows about position »use add() from ListIterator to add at a position »use remove() from ListIterator to remove at a position LinkedList knows a few things too »void addFirst(Object o), void addLast(Object o) »Object getFirst(), Object getLast() »Object removeFirst(), Object removeLast()

3-February-2003cse Collections © 2003 University of Washington21 Set Interface Context Collection Set

3-February-2003cse Collections © 2003 University of Washington22 Set Interface Same methods as Collection »different contract - no duplicate entries Defines two fundamental methods »boolean add(Object o) - reject duplicates »Iterator iterator() Provides an Iterator to step through the elements in the Set »No guaranteed order in the basic Set interface »There is a SortedSet interface that extends Set

3-February-2003cse Collections © 2003 University of Washington23 HashSet and TreeSet Context HashSetTreeSet Collection Set

3-February-2003cse Collections © 2003 University of Washington24 HashSet Find and add elements very quickly »uses hashing implementation in HashMap Hashing uses an array of linked lists »The hashCode() is used to index into the array »Then equals() is used to determine if element is in the (short) list of elements at that index No order imposed on elements The hashCode() method and the equals() method must be compatible »if two objects are equal, they must have the same hashCode() value

3-February-2003cse Collections © 2003 University of Washington25 TreeSet Elements can be inserted in any order The TreeSet stores them in order »Red-Black Trees out of Cormen-Leiserson-Rivest An iterator always presents them in order Default order is defined by natural order »objects implement the Comparable interface »TreeSet uses compareTo(Object o) to sort Can use a different Comparator »provide Comparator to the TreeSet constructor

3-February-2003cse Collections © 2003 University of Washington26 Map Interface Context Map

3-February-2003cse Collections © 2003 University of Washington27 Map Interface Stores key/value pairs Maps from the key to the value Keys are unique »a single key only appears once in the Map »a key can map to only one value Values do not have to be unique

3-February-2003cse Collections © 2003 University of Washington28 Map methods Object put(Object key, Object value) Object get(Object key) Object remove(Object key) boolean containsKey(Object key) boolean containsValue(Object value) int size() boolean isEmpty()

3-February-2003cse Collections © 2003 University of Washington29 Map views A means of iterating over the keys and values in a Map Set keySet() »returns the Set of keys contained in the Map Collection values() »returns the Collection of values contained in the Map. This Collection is not a Set, as multiple keys can map to the same value. Set entrySet() »returns the Set of key-value pairs contained in the Map. The Map interface provides a small nested interface called Map.Entry that is the type of the elements in this Set.

3-February-2003cse Collections © 2003 University of Washington30 HashMap and TreeMap Context HashMapTreeMap Map

3-February-2003cse Collections © 2003 University of Washington31 HashMap and TreeMap HashMap »The keys are a set - unique, unordered »Fast TreeMap »The keys are a set - unique, ordered »Same options for ordering as a TreeSet Natural order (Comparable, compareTo(Object)) Special order (Comparator, compare(Object, Object))

3-February-2003cse Collections © 2003 University of Washington32 Bulk Operations In addition to the basic operations, a Collection may provide “bulk” operations boolean containsAll(Collection c); boolean addAll(Collection c); // Optional boolean removeAll(Collection c); // Optional boolean retainAll(Collection c); // Optional void clear(); // Optional Object[] toArray(); Object[] toArray(Object a[]);

3-February-2003cse Collections © 2003 University of Washington33 Utilities Context

3-February-2003cse Collections © 2003 University of Washington34 Utilities The Collections class provides a number of static methods for fundamental algorithms Most operate on Lists, some on all Collections »Sort, Search, Shuffle »Reverse, fill, copy »Min, max Wrappers »synchronized Collections, Lists, Sets, etc »unmodifiable Collections, Lists, Sets, etc

3-February-2003cse Collections © 2003 University of Washington35 Appendix

3-February-2003cse Collections © 2003 University of Washington36 Legacy classes Still available Don’t use for new development »unless you have to, eg, J2ME, J2EE in some cases Retrofitted into Collections framework Hashtable »use HashMap Enumeration »use Collections and Iterators »if needed, can get an Enumeration with Collections.enumeration(Collection c)

3-February-2003cse Collections © 2003 University of Washington37 More Legacy classes Vector »use ArrayList Stack »use LinkedList BitSet »use ArrayList of boolean, unless you can’t stand the thought of the wasted space Properties »legacies are sometimes hard to walk away from … »see next few pages

3-February-2003cse Collections © 2003 University of Washington38 Properties class Located in java.util package Special case of Hashtable »Keys and values are Strings »Tables can be saved to/loaded from file

3-February-2003cse Collections © 2003 University of Washington39 System properties Java VM maintains set of properties that define system environment »Set when VM is initialized »Includes information about current user, VM version, Java environment, and OS configuration Properties prop = System.getProperties(); Enumeration e = prop.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); System.out.println(key + " value is " + prop.getProperty(key)); }