Object Oriented Design with UML and Java Part VII: Java Collection Classes

Similar documents
JAVA COLLECTIONS FRAMEWORK

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

Class 32: The Java Collections Framework

Data Structures in the Java API

Working with Java Collections

The Java Collections Framework

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Basic Programming and PC Skills: Basic Programming and PC Skills:

Software Engineering II. Java 2 Collections. Bartosz Walter <Bartek.Walter@man.poznan.pl>

Generic Types in Java. Example. Another Example. Type Casting. An Aside: Autoboxing 4/16/2013 GENERIC TYPES AND THE JAVA COLLECTIONS FRAMEWORK.

Java Interview Questions and Answers

Datastrukturer och standardalgoritmer i Java

Java Coding Practices for Improved Application Performance

What s Wrong with This?

Useful Java Collections

Lecture 2: Data Structures Steven Skiena. skiena

Java SE 8 Programming

Java Map and Set collections

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Practical Session 4 Java Collections

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Per imparare a programmare bisogna programmare

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Crash Course in Java

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Contents. 9-1 Copyright (c) N. Afshartous

CMSC 132: Object-Oriented Programming II. Design Patterns I. Department of Computer Science University of Maryland, College Park

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

core 2 Basic Java Syntax

CSC 551: Web Programming. Fall 2001

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Moving from CS 61A Scheme to CS 61B Java

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Introduction to Programming System Design. CSCI 455x (4 Units)

STORM. Simulation TOol for Real-time Multiprocessor scheduling. Designer Guide V3.3.1 September 2009

J a v a Quiz (Unit 3, Test 0 Practice)

CMSC 202H. ArrayList, Multidimensional Arrays

Object Oriented Software Design

Principles of Software Construction: Objects, Design and Concurrency. Java Collections. toad Fall 2013

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Chapter 8: Bags and Sets

Semantic Analysis: Types and Type Checking

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Getting Started with the Internet Communications Engine

C# Cookbook. Stephen Teilhet andjay Hilyard. O'REILLY 8 Beijing Cambridge Farnham Köln Paris Sebastopol Taipei Tokyo '"J""'

C++FA 5.1 PRACTICE MID-TERM EXAM

Collections and iterators

Software Engineering Techniques

e ag u g an L g ter lvin v E ram Neal G g ro va P Ja

dictionary find definition word definition book index find relevant pages term list of page numbers

LINKED DATA STRUCTURES

Computer Programming I

Data Structures in Java. Session 15 Instructor: Bert Huang

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Big Data & Scripting Part II Streaming Algorithms

Java Collection Framework hierarchy. What is Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues

Specialized Programme on Web Application Development using Open Source Tools

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CS 111 Classes I 1. Software Organization View to this point:

Project 4 DB A Simple database program

C++ INTERVIEW QUESTIONS

Specialized Programme on Web Application Development using Open Source Tools

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Collections Classes for Real-Time and High-Performance Applications. July 4, 2005 Jean-Marie Dautelle

Computer. Course Description

Java the UML Way: Integrating Object-Oriented Design and Programming

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Data Structures and Algorithms

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Java from a C perspective. Plan

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

CS 106 Introduction to Computer Science I

Programming Languages CIS 443

In this Chapter you ll learn:

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Fundamentals of Java Programming

Android Application Development Course Program

To Java SE 8, and Beyond (Plan B)

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Sequential Data Structures

AP Computer Science Java Subset

CSE 403. Performance Profiling Marty Stepp

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

The Interface Concept

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Variables, Constants, and Data Types

Structural Design Patterns Used in Data Structures Implementation

Java (12 Weeks) Introduction to Java Programming Language

Java EE Web Development Course Program

Transcription:

Object Oriented Design with UML and Java Part VII: Java Collection Classes Copyright David Leberknight and Ron LeMaster. Version 2011

Collection Classes A collection is a grouping of objects, usually of the same class, or with a common base class. Used to represent the many side of one-to-many associations, where the association must be navigable from the one side to the many side: aggregations compositions Excellent support in most OO languages: The java.util package The java.util.concurrent package STL for C++ (Standard Template Library) ( VII ) Java Collection Classes - 2

Collection Example Each one-to-many association implies a collection attribute. Student Course Assignment 0..n 0..n 0..n FacultyMember ( VII ) Java Collection Classes - 3

Java Collections Pre JDK 1.2 Vector (ported to 1.2 Collection framework) Hashtable (ported to 1.2 Collection framework) JDK 1.2 through JDK 1.4 JDK 1.2 Collections are typeless; they hold Objects, and you must downcast the results of operations on them. More uniform class structure Wider variety of collections Introduces Iterator design pattern JDK 1.5 + Generics! (analogous to C++ templates) It s about time :-) ( VII ) Java Collection Classes - 4

Java Collections (JDK 1.2 to 1.4) <<interface>> Collection <<interface>> Iterator <<interface>> List <<interface>> Set keys <<interface>> Map <<abstract>> AbstractList <<abstract>> AbstractSet <<interface>> SortedSet <<ported>> Hashtable <<abstract>> AbstractMap <<interface>> SortedMap <<ported>> Vector ArrayList LinkedList HashSet TreeSet HashMap TreeMap ( VII ) Java Collection Classes - 5

Collection Considerations There are many different types of collections. Choosing the best one for the job requires careful consideration. Not all OO languages support all types of collections. The C++ Standard Template Library (STL) does not support HashTable, but it does support TreeMap. Prior to version 1.2, Java did not directly support sorted collections. Consider the operations you need to make on the collection, because each type performs some operations better (more efficiently) than others: Adding / removing / searching for an element Sorting Iterating ( VII ) Java Collection Classes - 6

The Java Collections Framework The Java Collections Framework (JDK version 1.2+) provides utilities to help with such things as thread safety, sorting and reversing a list. Classes with convenience methods for functions such as sorting: Collections Arrays Iterfaces for ordered collections ( TreeSet & TreeMap ): Comparable Comparator One other important interface: Iterator ( VII ) Java Collection Classes - 7

The Collection Interface (JDK 1.2) public interface Collection { public boolean add( Object e ); public void clear(); public boolean contains( Object e ); public boolean isempty(); public Iterator iterator(); public boolean remove( Object e ); public int size(); public Object[] toarray();... ( VII ) Java Collection Classes - 8

Java Generics (JDK 1.5 +) Generics provide increased type safety and expressiveness. // Bad example avoid this usage: private final Collection foos =...; // collection of Foos? foos.add( new Bar() ); // Wrong, but compiles and runs for( Iterator i = foos.iterator(); i.hasnext(); ) { Foo foo = (Foo) i.next(); // throws ClassCastException // A better way: private final Collection< Foo > foos = new ArrayList< Foo >(); foos.add( new Bar() ); // Will not compile unless Bar is-a Foo for( Foo foo : foos ) { // No downcast required. // Easy syntax, type safety, all good :-) ( VII ) Java Collection Classes - 9

The Collection Interface (JDK 1.5) public interface Collection< E > extends Iterable< E > { public boolean add( E o ); public void clear(); public boolean contains( Object o ); public boolean isempty(); public Iterator< E > iterator(); public boolean remove( Object o ); public int size(); public Object[] toarray(); ( VII ) Java Collection Classes - 10

Primitive Arrays vs. Collections Java and C++ both have arrays at the language-syntax level. A type of indexed collection. Size fixed at creation time. The programmer must handle out-of-bounds situations. C++ has tricky array pitfalls - better not to use them. String[] oolanguages = new String[] { C++, Java, Smalltalk, C#, JADE, Python, Lisp ; List is a Collection Class. Dynamically sized array. No out-of-bounds issues. Supports the Iterator design pattern. ( VII ) Java Collection Classes - 11

java.util.arraylist (JDK 1.2) public class ArrayList implements List { public ArrayList(); public void add( Object o ); public Object remove( int index ); public Object get( int index ); public int size(); public Object[] toarray();... ArrayList al = new ArrayList(); al.add( new MyClass() ); MyClass mc = (MyClass) al.get( 0 ); // Downcast required ( VII ) Java Collection Classes - 12

java.util.arraylist (JDK 1.5) public class ArrayList< E > extends AbstractList< E > implements List< E >,... Cloneable, Serializable { public ArrayList(); public ArrayList( Collection<? extends E > c ); public boolean add( E o ); public E remove( int index ); public E get( int index ); public int size(); // <? Extends E > means: an unknown type that is a subtype of E, possibly E itself. This is an example of a bounded wildcard. ArrayList< MyClass > al = new ArrayList< MyClass >(); al.add( new MyClass() ); MyClass mc = al.get( 0 ); // NO Downcast required!! ( VII ) Java Collection Classes - 13

The Iterator Design Pattern Intent: Isolate the iteration over a collection, so that multiple iterations can progress concurrently, and so that different iteration strategies might be used interchangeably. Iterators are tightly bound to a single collection, allowing you to move through the collection sequentially. Searching for an element may return an iterator if more than one element satisfies the search criteria. This is the case for example, with database queries using Java s JDBC (refer to java.sql.resultset). Iterators can enable more than one client to safely operate on a collection at once. Use java.util.concurrent.* ( VII ) Java Collection Classes - 14

Iterator as good design The Client class remains blissfully ignorant of the actual class that implements the Iterator interface. <<interface>> Iterator +hasnext() : boolean +next() : Object Client java.util.arraylist +iterator():iterator ArrayListIterator ( VII ) Java Collection Classes - 15

java.util.iterator An interface for an object that moves sequentially through a collection. Each concrete collection class has its own implementation of Iterator. The only way to get an Iterator instance is by calling the iterator() method on a Collection, or keyset().iterator() on a Map. If you add to or delete from a collection while iterating through it, your iterator might no longer be valid (be careful!). Use it.remove(); public interface Iterator< E > { public boolean hasnext(); public E next(); public void remove(); ( VII ) Java Collection Classes - 16

java.util.iterator Example public void dofoo( List< Foo > listoffoos ) { Iterator< Foo > fit = listoffoos.iterator(); while( fit.hasnext() ) { Foo foo = fit.next(); // No downcast if( bogusfoo( foo ) ) { fit.remove(); // Safe removal ( VII ) Java Collection Classes - 17

Associative Arrays Very important collection class, also called dictionaries or maps Contains key / value pairs Like a normal array, but indexed by key, rather than by sequential integer. Support in Java: java.util.hashtable ( JDK 1.0+ ) java.util.hashmap / TreeMap ( JDK 1.2+ ) java.util.concurrent.concurrenthashmap ( JDK 1.5+ ) HashMap< String, PhoneNum > phonebook = new HashMap< String, PhoneNum >(); phonebook.put( Dupp, Jack, new PhoneNum( 720-555-9354 )); phonebook.put( Lucks, Dee, new PhoneNum( 303-555-1764 )); PhoneNum number = phonebook.get( Lucks, Dee ); ( VII ) Java Collection Classes - 18

Associative Arrays JDK 1.2 usage import java.util.hashmap; class MapTest { public static void main( String[] args ) { HashMap map = new HashMap(); // JDK 1.2 usage map.put( a, b ); map.put( a, c ); Object o = map.get( a ); String s = (String) o; // Downcast - Pre-1.5 usage System.out.println( key = a; value = + s ); // outputs: key = a; value = c ( VII ) Java Collection Classes - 19

Hash Table data structure Hash table data structures have been around for decades, mainly as a fast associative array, and in places other than OO containers. A hash function is defined on the keys. The hash function assigns the value of each key to one of a fixed number of buckets (computed in constant time no search). Based on the key, the hash function computes a integer value between 0 and the number of buckets - 1.» For an integer type key, the hash function could return the key modulo the number of buckets.» For a string type key, the hash function could add the ASCII values of all the characters, then return the sum modulo the number of buckets. Hash values are not unique across the range of possible keys. When two keys produce the same hash values, they are said to collide. Values with colliding keys are put into a linked list for the bucket. ( VII ) Java Collection Classes - 20

Hashtable and HashMap data structures Pros: Cons: Useful dictionary class with a simple API: put() & get() Performs well if you have a good hash function, especially if there are a very large number of keys (compared to searching). Has poor performance if the hash function produces many key collisions (mapping to the same bucket). Not sortable, so Iterators created from the keyset() return entries in random order - Compare to TreeMap. ( VII ) Java Collection Classes - 21

java.util.hashmap (JDK 1.2) public class HashMap extends AbstractMap { public void clear(); public boolean containskey( Object key ); public boolean containsvlaue( Object value ); public Set entryset(); public Object get( Object key ); public boolean isempty(); public Set keyset(); public void put( Object key, Object value ); public Object remove( Object key ); public int size(); public Collection values(); ( VII ) Java Collection Classes - 22

java.util.hashmap HashMap doesn t implement the Collection interface, so to get an Iterator, you must call keyset(), entryset(), or values() to get a Collection, and then iterate over that Collection. Not thread-safe, for increased performance. But the Collections utility class provides a synchronizedmap( Map m ) method, wrapping the given Map, providing optional thread safety. Or use java.util.concurrent.concurrenthashmap. Hash functions: Object provides a default hash function based on the instance s location in memory, and so puts and gets from the map are based on object identity. This is what you want in most instances. String has a hash function based on the characters in the string, and so puts and gets from the map are based on value equality. This is also what you usually want. ( VII ) Java Collection Classes - 23

Map Considerations In Java, keys and values must be Objects, not primitive types. Although with Java 5 there is auto-boxing. For Objects that are used as keys in hash maps, a test for equality is needed. It is common to override Object s equals() method. For TreeMaps (and TreeSets), a sort order function is required, accomplished with Comparator or Comparable. Get used to using Maps!!! The availability of associative arrays can transform the way you write programs. ( VII ) Java Collection Classes - 24

Qualified Associations Qualified associations usually imply associative arrays StockCertificate Company 1..n StockHolder -certificateid : String 0..n -taxpayerid : SSN StockCertificate -certificateid : String certificateid Company taxpayerid StockHolder -taxpayerid : SSN ( VII ) Java Collection Classes - 25

Tree Map Maintains elements in a type of balanced tree. Pros and cons similar to hash table, plus Tree maps keep the elements in sorted order, which can be useful. Sorting requires that all elements implement the Comparable interface, or that the TreeMap be constructed with an appropriate Comparator. The C++ Standard Template Library (STL) chose to base its associative arrays on trees rather than hash tables. Smalltalk implements its Dictionary class with hash tables. Java provides both HashMap and TreeMap. ( VII ) Java Collection Classes - 26

Comparable & Comparator (JDK 1.2) // return -1, 0, or 1 interface Comparable { public int compareto( Object other ); interface Comparator { public int compare( Object o1, Object o2 ); ( VII ) Java Collection Classes - 27

Comparable Example (JDK 1.2) import java.util.*; public class CollectionTest implements Comparable { private int value = 0; public int compareto( Object other ) { int othervalue = ((CollectionTest) other).getvalue(); if( this.value == othervalue ) return 0; return ( this.value > othervalue )? 1 : -1; private int getvalue() { return value; public int hashcode() { return value; ( VII ) Java Collection Classes - 28

Comparable Example (cont.) private void setvalue( int value ) { this.value = value; public String tostring() { return "( " + value + " )"; public static void printmap( Map map ) { Set keys = map.keyset(); Iterator it = keys.iterator(); while( it.hasnext() ) { Object key = it.next(); Object value = map.get( key ); System.out.print( "Key = " + key ); System.out.println( " Value = " + value ); ( VII ) Java Collection Classes - 29

Comparable Example (cont.) public static void main( String[] args ) { CollectionTest[] cts = new CollectionTest[ 5 ]; HashMap hash = new HashMap(); TreeMap tree = new TreeMap(); Random random = new Random( System.currentTimeMillis() ); for( int i = 0; i < 4; i++ ) { CollectionTest ct = new CollectionTest(); cts[ i ] = ct; int randy = Math.abs( random.nextint() ) % 100; ct.setvalue( randy ); hash.put( ct, "" + i ); tree.put( ct, "" + i ); List list = Arrays.asList( cts ); System.out.println( "HASH dump:" ); printmap( hash ); System.out.println( "TREE dump:" ); printmap( tree ); ( VII ) Java Collection Classes - 30

Comparable Example - output Note that 36 is duplicate in the HashMap but not in the TreeMap TreeMap uses compareto() to test for equality. HashMap relies on equals(), which CollectionTest does not override; it therefore tests for object identity, not the numeric value. HASH dump: Key = ( 75 ) Value = 1 Key = ( 39 ) Value = 0 Key = ( 36 ) Value = 3 Key = ( 36 ) Value = 2 TREE dump: Key = ( 36 ) Value = 3 Key = ( 39 ) Value = 0 Key = ( 75 ) Value = 1 ( VII ) Java Collection Classes - 31

Another Collections Example import java.util.*; public class CollectionsExample { public static void main( String args[] ) { // Print a list of ints in reverse sorted order. List< Integer > listofints = new LinkedList< Integer >(); listofints.add( 7 ); // Note the use of auto-boxing listofints.add( 3 ); listofints.add( 11 ); Comparator< Integer > reverse = Collections.reverseOrder(); Collections.sort( listofints, reverse ); for( int i : listofints ) { // more auto-boxing System.out.println( i ); ( VII ) Java Collection Classes - 32

Sets A set is a collection of unique objects (there are no duplicates). Supported in Java (1.2) with HashSet and TreeSet. Useful for determining membership. // Set of customers with outstanding charges is kept in memory: HashSet custwithcharges = new HashSet(); // Somewhere else, in code that checks for such things if( custwithcharges.contains( cust.getid() ) { return false; ( VII ) Java Collection Classes - 33

Other Java Collections LinkedList A doubly-linked list. Stack WeakHashMap Entries are removed when their key is no longer in use. A weak reference is not counted as a reference in garbage collection. BitSet Vector of bits that grows as needed. Properties Set of properties (usually used for.ini type files). Refer to java.util.concurrent for such classes as: BlockingQueue - Blocks or times out when adding to a full queue or removing from an empty queue. ConcurrentMap<K,V> - Adds atomic methods: ( VII ) Java Collection Classes - 34