Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1


Ratings: (0)|Views: 15 |Likes:
Published by api-3724182

More info:

Published by: api-3724182 on Oct 18, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





New Java features
Java 5.0 introduced (at least) three language features
Automatically converts primitives (such asi nt) to wrapper classes (such as
Enhancedfor loop
New concise syntax for iterating through a collection
Constrain what kinds of objects collections such asV e c t ors,A r r a y Li s ts,
Maps, and Sets can contain
Removes need for annoying typecasts and instanceof checks
This automatically creates anInteger object:
Integer i1 = 3; // autoboxing
int i2 = i1; // unboxing
Note that this doesn\u2019t have a call toi nt V a l ue:
/** Return the sum of the elements in a */

public static int sum(Integer[] a) {
int result = 0;
for (int i = 0; i != a.length; i++) {

result += a[i]; // unboxing
}return result;
Enhancedf or loop
Removes need for indexing:
/** Return the sum of the elements in a */

public static int sum(Integer[] a) {
int result = 0;
for (Integer i : a) {

result += i;
}return result;
(Sure beats the old way, doesn\u2019t it?)
Enhancedf or loop (cont.)
Another example, using anAr r a y L is t instead of an array (this isa lmo s t right \u2013 we
will correct it in a few slides from now):
/** Return the sum of the elements in a */

public static int sum(ArrayList a) {
int result = 0;
for (Integer v : a) {

result += v;
}return result;
The old way:
Iterator iter = a.iterator();
while (iter.hasNext()) {
Integer v = (Integer) iter.next();
Java: generics
Java 1.4:
When you take something out of a collection (such as
Vector) you must cast it
You can\u2019t say "I want a list ofD a tes"; instead, you must
work withO b je c ts
New feature:g e n er ic s
Java 1.5 generics:
Can specify what type a collection should containwh e n
you declare it
Can only insert objects of that type
No need to cast when you get them back out
Java: generics
Examples of collections using generics:
java.util.Set<E>, java.util.List<E>
The< E> specifies a generic type
Can declareS e ts andL is ts of whatever we want
A list that can only holdSt ri n gs:

List<String> a = new ArrayList<String>();
a.add("Look ma, no cast on the next line");
String result = a.get(0);
a.add(new Date()); // compile error!

Java: generics
Iteration using generics, autoboxing, and the new for loop is beautiful (to
a geek):
/** Return the sum of the elements in a */

public static int sum(ArrayList<Integer> a) {
int result = 0;
for (Integer v : a) {

result += v;
}return result;
Java: generics (cont.)
New safety and ease:

Can only callsum with lists that contain onlyIn t e g e rs
No need for a typecast or a call toin t V a l ue
No need to create anI t e ra t o r

Want to associate pairs of values where one of the
values is guaranteed to be unique
Example: match people with their favourite chocolate
"Michelle" => "Coffee Crisp"
"Paul" => "Kit Kat"
How would we do this with lists?
A common problem
A list is a function from0 . . N - 1 to values
As a list of pairs:
[["Darwin", "Snickers"],["Newton",
"Mars Bar"],["Turing", "Kit Kat"]]
A map is a function from keys to values
As a map:
{"Newton"="Mars Bar", "Darwin"=
"Snickers", "Turing"="Kit Kat"}
Note: Maps areuno rder ed
Solution: maps
Each key can appear at most once and has only one
Also calledhas hes (Perl),di c ti onari es (Python), and
associative arrays(ancient)
Generic properties of maps defined by interface
java.util.Map<K, V>: a Java interface that maps keys
of type< K> to values of type< V >
Classes implementingM a p:
HashMap, TreeMap
Take CSC263H to learn how to choose
Interface and implementation

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->