1. Trinity College
Java Review Part II
Timothy Richards
Trinity College, Hartford CT • Department of Computer Science • CPSC 225
2. Java API
The entire Java 1.6 API documentation
is available on the web
http://download.oracle.com/javase/6/docs/api/
Use It!
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 2
3. java.util
Provides off-the-shelf data structures
that you can use in your programs
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 3
4. java.util
Provides off-the-shelf data structures
that you can use in your programs
List Map Set
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 4
5. java.util
Provides off-the-shelf data structures
that you can use in your programs
List Map Set
These are not classes!
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 5
6. java.util
Provides off-the-shelf data structures
that you can use in your programs
List Map Set
They are interfaces
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 6
7. java.util
Provides off-the-shelf data structures
that you can use in your programs
List Map Set
They are interfaces
What’s an interface?
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 7
8. java.util
Provides off-the-shelf data structures
that you can use in your programs
List Map Set
LinkedList HashMap HashSet
ArrayList Hashtable LinkedHashSet
Stack Properties TreeSet
Vector TreeMap
Queue
Class Implementations
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 8
9. java.util
Provides off-the-shelf data structures
that you can use in your programs
List Map Set
LinkedList HashMap HashSet
ArrayList Hashtable LinkedHashSet
Stack Properties TreeSet
Vector TreeMap
Queue
Class Implementations
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 9
10. Generics
How do I use a HashMap?
HashMap m = new HashMap();
m.put(“Tim”, 5);
m.get(“Tim”);
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 10
11. Generics
How do I use a HashMap?
HashMap m = new HashMap();
m.put(“Tim”, 5);
m.get(“Tim”);
This is OK...
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 11
12. Generics
How do I use a HashMap?
Map m = new HashMap();
m.put(“Tim”, 5);
m.get(“Tim”);
Here we use
an interface This is better...
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 12
13. Generics
How do I use a HashMap?
Map m = new HashMap();
m.put(“Tim”, 5);
m.get(“Tim”);
Here we use
an interface This is better...
It separates the
interface from the
implementation
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 13
14. Generics
How do I use a HashMap?
Map m = new HashMap();
m.put(“Tim”, 5);
m.get(“Tim”);
Question?
What does the call to
this method return?
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 14
15. Generics
How do I use a HashMap?
Map m = new HashMap();
m.put(“Tim”, 5);
Object r = m.get(“Tim”);
Question?
It returns an Object!
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 15
16. Generics
How do I use a HashMap?
Map m = new HashMap();
m.put(“Tim”, 5);
Object r = m.get(“Tim”);
int i = (Integer) r;
Question?
That means we need to
“cast” it to the correct
type...
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 16
17. Generics
How do I use a HashMap?
Map m = new HashMap();
m.put(“Tim”, 5);
Object r = m.get(“Tim”);
int i = (Integer) r;
Question?
But, I thought it was an
int?
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 17
18. Generics
How do I use a HashMap?
Map m = new HashMap();
m.put(“Tim”, 5);
Object r = m.get(“Tim”);
int i = (Integer) r;
Question?
Yes, but Java is clever enough to know that if you
need an int, an Integer will be “converted” to an
int, and vice versa.
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 18
19. Generics
How do I use a HashMap?
Map m = new HashMap();
m.put(“Tim”, 5);
Object r = m.get(“Tim”);
int i = (Integer) r; This Works!
int x = new Integer(5);
Question? Integer y = x;
Yes, but Java is clever enough to know that if you
need an int, an Integer will be “converted” to an
int, and vice versa.
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 19
20. Generics
How do I use a HashMap?
Map m = new HashMap();
m.put(“Tim”, 5);
Object r = m.get(“Tim”);
int i = (Integer) r;
So, does this mean I need to do this every time I
get something from a Map?
Yes... unless you use generic types!
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 20
21. Generics
How do I use a HashMap?
Map m = new HashMap();
m.put(“Tim”, 5);
Object r = m.get(“Tim”);
int i = (Integer) r;
What is a generic type?
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 21
22. Generics
How do I use a HashMap?
Map<String,Integer> m = new HashMap<String,Integer>();
m.put(“Tim”, 5);
Integer r = m.get(“Tim”);
int i = (Integer) r; No longer necessary
What is a generic type?
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 22
23. Generics
How do I use a HashMap?
Map<String,Integer> m = new HashMap<String,Integer>();
m.put(“Tim”, 5);
Integer r = m.get(“Tim”);
int i = (Integer) r; No longer necessary
All the Java util classes can be used in this way.
For example...
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 23
24. ForEach Loop
Now, a different topic...
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 24
25. ForEach Loop
Now, a different topic...
We know about one kind of for loop
bounded iteration
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 25
26. ForEach Loop
Now, a different topic...
We know about one kind of for loop
bounded iteration
I mentioned there was another...
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 26
27. ForEach Loop
Imagine we want to iterate over
the elements in a list
List<String> x = new ArrayList<String>();
x.add(“Tom”);
x.add(“Jerry”);
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 27
28. ForEach Loop
Imagine we want to iterate over
the elements in a list
List<String> x = new ArrayList<String>();
x.add(“Tom”);
x.add(“Jerry”);
for (int i = 0; i < s.size(); i++)
s.get(i);
Here is one way to achieve that
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 28
29. ForEach Loop
Imagine we want to iterate over
the elements in a list
List<String> x = new ArrayList<String>();
x.add(“Tom”);
x.add(“Jerry”);
Iterator<String> i = x.iterator();
while (i.hasNext())
i.next()
Here is another way to achieve that
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 29
30. ForEach Loop
Imagine we want to iterate over
the elements in a list
List<String> x = new ArrayList<String>();
x.add(“Tom”);
x.add(“Jerry”);
for (String e : x)
// do something with e...
Here is the right way to achieve that
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 30
31. ForEach Loop
Imagine we want to iterate over
the elements in a list
List<String> x = new ArrayList<String>();
x.add(“Tom”);
x.add(“Jerry”);
for (String e : x)
// do something with e...
Here is the right way to achieve that
for each string e in x...
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 31
32. Any Questions?
We covered quite a bit of Java
in the past two lectures...
Does anyone have any questions?
Trinity College, Hartford CT • Department of Computer Science • CPSC 225 32