practical session 4 java collections
DESCRIPTION
Practical Session 4 Java Collections. Outline. Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack The Collections class Wrapper classes. Collection. A group of elements. The group size can be changed during run-time. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/1.jpg)
Practical Session 4Java Collections
![Page 2: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/2.jpg)
OutlineWorking with a Collection
The Collection interface
The Collection hierarchy
Case Study: Undoable Stack
The Collections class
Wrapper classes
![Page 3: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/3.jpg)
Collection
A group of elements. The group size can be changed during run-time.
A collection object has many useful methods for manipulating the collection:
• Inserting elements
• Deleting elements
• Copying the collection
• Iterating over the elements
• Computing subsets of elements
…
Various types of collections are defined in the standard library:
Vector, LinkedList, Stack, ArrayDequeue, PriorityQueue, TreeSet,…
![Page 4: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/4.jpg)
Collection
Why shouldn’t we just use arrays?
• Arrays are not objects!
• You cannot invoke methods on arrays.
• Arrays have fixed size.
• Arrays are less convenient for representing certain types of collections:
• Sets
• Double-ended queues
• Dictionaries
• …
![Page 5: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/5.jpg)
Working with a collection
import java.util.Vector;
…
Vector <Car> carsVec = new Vector <Car>();
Car Volvo = new Car(2.0);
Car BMW = new Car(2.2);
carsVec.add(Volvo);
carsVec.add(BMW);
for (Car c: carsVec)
System.out.println(c);
Definition syntax: Collection <type> colName = new Collection <type>()
Example:
![Page 6: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/6.jpg)
Working with a collection
Car temp = carsVec.elementAt(0);
carsVec.set(0,carsVec.elementAt(1));
carsVec.set(1, temp);
for (int i=0; i < carsVec.size(); i++)
System.out.println(carsVec.elementAt(i));
Example - continued:
![Page 7: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/7.jpg)
The Collection InterfaceCollection is an interface in the standard java library.
The Collection interface is generic. It accepts a type as a parameter.
public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element); //optional
boolean remove(Object element); //optional
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] a);
…
![Page 8: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/8.jpg)
The Collection Hierarchy
Collection
List Set Queue
(Partial illustration)
![Page 9: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/9.jpg)
The Collection Hierarchy
Collection
List Set Queue
Vector
(Partial illustration)
![Page 10: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/10.jpg)
The Collection Hierarchy
Collection
List Set Queue
Vector
Stack
LinkedList PriorityQueue
(Partial illustration)
![Page 11: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/11.jpg)
Case Study: Undoable Stack
Various programs allow the user to undo his operations.
The undo operations are performed in reverse order.
In such a program, we need to add each operation with its parameters
onto the stack.
![Page 12: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/12.jpg)
Program Stack
![Page 13: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/13.jpg)
Program Stack
Resize 36
![Page 14: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/14.jpg)
Program Stack
Resize 36
Resize 36
Recolor 4
![Page 15: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/15.jpg)
Program Stack
Resize 36
Recolor 4
Resize 24
Undo:
Resize(36)
![Page 16: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/16.jpg)
Program Stack
Resize 36
Recolor 4
Resize 24
Resize 36
Recolor 4
Undo:
Resize(36)
![Page 17: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/17.jpg)
Undoable Stack Hierarchy
![Page 18: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/18.jpg)
Undoable Stack Hierarchy
TextArea
-text: String
-size: int
-color: int
+ getters…
+ setters…
![Page 19: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/19.jpg)
Undoable Stack Hierarchy
Operation
+ perform(arg: int)
+ getArg() : int
TextArea
-text: String
-size: int
-color: int
+ getters…
+ setters…
![Page 20: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/20.jpg)
Undoable Stack Hierarchy
Operation
+ perform(arg: int)
+ getArg() : int
Recolor
- color: int
+ perform(arg: int)
+ getArg() : int
Resize
- size: int
+ perform(arg: int)
+ getArg() : int
TextArea
-text: String
-size: int
-color: int
+ getters…
+ setters…
![Page 21: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/21.jpg)
Undoable Stack Hierarchy
Operation
+ perform(arg: int)
+ getArg() : int
Recolor
- color: int
+ perform(arg: int)
+ getArg() : int
Resize
- size: int
+ perform(arg: int)
+ getArg() : int
TextArea
-text: String
-size: int
-color: int
+ getters…
+ setters…
UndoStack
+ add (op :Operation)
+ undo()
0..n
![Page 22: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/22.jpg)
The Collections classThe Collections class contains static methods that operate on collections:
• min
• max
• reverse
• sort
• …
Example:
import java.util.Collections; import java.util.Vector;
…
Vector <Car> carsVec = new Vector <Car>();
…
Collections.reverse(carsVec);
![Page 23: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/23.jpg)
Wrapper Classes
What happens if we want to create a collection of a primitive type?
Collections can be made only of objects.
Primitive types are not objects.
We use wrapper classes instead.
A wrapper class is an object representing a primitive type.
![Page 24: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/24.jpg)
Wrapper Classes
int
float
double
char
boolean
Integer
Float
Double
Character
Boolean
Example:Integer intObj = new Integer(10);int number = intObj.intValue();
![Page 25: Practical Session 4 Java Collections](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56813196550346895d9808db/html5/thumbnails/25.jpg)
A collection of wrapper objects
Import java.util.Vector;
…
Vector <Integer> numVec = new Vector <Integer>();
int size = 10;
for (int i = size; i >0; i--)
numVec.add(new Integer( i ));
Collections.sort(numVec);
for (Integer i: numVec)
System.out.println( i);