C SC 205 Lecture 5: Java 5/6 and Collections Framework
major resources: Data Structures and the Java Collections Framework Second Edition,
William Collins, McGraw-Hill, 2005
Introduction to Programming and OO Design
using Java, Niño and Hosch, Wiley & Sons, 2002
[ previous
| schedule
| next ]
Monday September 24 (week 3)
Preliminary: iterators
- technique for traversing a collection
- used ArrayList as an example
- basic usage: request iterator from collection, then use hasNext() and next()
- compare and constrast: iterator + while loop versus get() + basic for loop
- syntax
- collection's underlying data structure: array or linked
- effect of data structure on performance: next() versus get()
- consider runtime properties
Java 1.5 and Java 5.0 name confusion
- Massive changes in 1.2 over 1.1 gave rise to "Java 2 platform" but retained 1.2 version number
- Versions 1.3 and 1.4 proved evolutionary so "Java 3" and "Java 4" were never used
- Massive changes in 1.5 over 1.4 gave rise to confusion e.g. "Java 2 platform SE 5.0"
- "Version "5.0" is the product version, while "1.5.0" is the developer version."
(from http://java.sun.com/j2se/1.5.0/docs/relnotes/version-5.0.html)
Java 5.0 goals: cleaner and more type-safe code
- major new and enhanced features promote these goals
- resistance to introducing new reserved words (can break existing code)
- influence of C++ (and C#?)
- details of new and enhanced features below
Java 5.0: The foreach construct
- Safe and syntactically clean traversal of array or collection
- Example: traverse an array or collection stuff whose elements are of type String
- old syntax for arrays:
for (int i=0; i<stuff.length; i++)
System.out.println(stuff[i].toLowerCase());
- old syntax for collection using iterator:
Iterator it = stuff.iterator();
while (it.hasNext())
System.out.println(((String)it.next()).toLowerCase());
- "foreach" syntax for either array or collection:
for (String element : stuff)
System.out.println(element.toLowerCase());
Java 5.0: Parameterized classes (generics)
- primarily benefit collection classes, and we'll focus on those
- allows programmer to specify collection element type at time of collection declaration/creation
- Example: ArrayList<String> names = new ArrayList<String>();
- type-safe because only type-compatible element goes in, comes out, or is referenced.
- cleaner syntax because type-casting is not needed to invoke referenced element's methods
- prior to 5.0, all elements going in, out, or referenced were of type Object
- accordingly, prior to 5.0, type-casting was needed in most situations when referencing a collection element
Other Java 5.0 features
- Type safe enumerated types: type whose constant fields represent all possible values (e.g. days of week)
- Example of how enumerated types were implemented previously: public static final objects with private constructors
- variable number of method parameters (a.k.a. varargs)
- the printf method for C-like formatted output
- the Scanner class for cleaner input from keyboard or file streams
- we studied code example incorporating all the above
Java 6
- "Java Platform, Standard Edition 6 (Java SE 6). The previous release was Java 2 Platform, Standard Edition 5.0"
(from http://java.sun.com/javase/namechange.html)
- internal developers version number is 1.6
- changes from 5 to 6 not very dramatic for student programmers so we did not get into detail
Overview of Java Collections Framework
- collection is object that contains multiple similar elements that can treated as a unit.
- framework is unified architecture.
- framework consists of interfaces, classes, algorithms
- Java Collections Framework tutorial: http://java.sun.com/docs/books/tutorial/collections/intro/index.html
- looked at pertinent subtrees of interface/class structures
- Collection interface, List subinterface, abstract and concrete list-oriented subclasses
- Collection interface, Set subinterface, abstract and concrete set-oriented subclasses
- Map interface and abstract and concrete map-oriented subclasses.
- lists allow duplicates, sets do not
- map elements consist of (key, value) pairs
- some classes allow elements/values to be null, others do not
- concrete subclasses are named according to implementation technique: array, linked list, tree, hash
- the Collections class provides numerous static utilities for working with collections (searching, sorting, etc)
- Collections is analogous to Arrays or Math in that regard
- we will spend a lot of time with this framework
[ C
SC 205 | Peter
Sanderson | Math Sciences server
| Math Sciences home page
| Otterbein ]
Last updated:
Peter Sanderson (PSanderson@otterbein.edu)