Download presentation
Presentation is loading. Please wait.
1
Practical Session 3 Java Collections
2
Outline Working with a Collection The Collection interface
The Collection hierarchy Case Study: Undoable Stack Maps The Collections class Wrapper classes
3
Collection A group of elements.
A collection object has many useful methods for manipulating the collection: Insert elements Delete elements Clear the collection Iterate over the elements (implements Iterable) Investigate the collection (getClass, size(), isEmpty(), contains(), …) … Java’s Collections in the standard library: Vector, ArrayList, Stack, ArrayDequeue, PriorityQueue, TreeSet, HashMap…
4
Collection Different implementations have different run-time needs
Why shouldn’t we just use arrays? Arrays have fixed size and can’t be extended Arrays are less convenient for representing certain types of collections: Sets Double-ended queues Dictionaries … Different implementations have different run-time needs Arrays are not objects!
5
Working with a collection
Definition syntax: Collection-class <type> colName = new Collection-class <type>(…) A class that implements Collection The collection elements type Can be any class type but NOT a primitive The name of the variable Optional. Can use the Diamond <> operator instead Constructor parameters list Example: Vector <Car> cars = new Vector <Car>(); ArrayQueue <Job> jobs = new ArrayQueue <>(10);
6
Working with a collection Example
import java.util.Vector; … Vector <Car> cars = new Vector <>(); cars.add(new Car(“Volvo”, 2.0)); cars.add(new Car(“BMW”, 2.2)); for (Car c: cars) System.out.println(c); 1 Car temp = cars.elementAt(0); cars.set(0,cars.elementAt(1)); cars.set(1, temp); for (int i=0; i < cars.size(); i++) System.out.println(cars.elementAt(i)); 2
7
The Collection Interface
Collection is a generic interface in the standard java library. public interface Collection<E> extends Iterable<E> { int size(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); Object[] toArray(); … See Java API Docs for full definition
8
The Collection Hierarchy
(Partial illustration) Collection Iterable List Set Queue Vector LinkedList PriorityQueue Stack
9
The Collection Hierarchy
10
Case Study: Undo Various programs allow the user to undo his operations The undo operations are performed in reverse order. Implementing such a program by adding each operation into a stack
11
Case Study: Undo Stack Action Type “hello world” Resize 44 Recolor red
Italic on Hello World
12
Case Study: Undo UML Operation + undo(TA: TextArea) TextArea
-text: String -size: int -color: int - italic: boolean + getters… + setters… UndoStack + add (op :Operation) + undo() 0..n BaseOperation<T> - oldValue: T + BaseOperation(vOld: T) Recolor - color: Integer + undo() Resize - size: Integer Text - text: String Italic - isOn: boolean UndoStack extends Stack (java)
13
Case Study: Undo UndoStack extends Stack (java)
private interface Operation { public void undo(TextArea TA); } private static abstract class BaseOperation<T> implements Operation { private T oldValue = null; public BaseOperation(T ov) {oldValue = ov;} public T getOldValue() { return oldValue;} } private static class Text extends BaseOperation<String> { public Text(String oValue) { super(oValue);} public void undo(TextArea TA) { System.out.println("Undo Text - Set it back to text'" + getOldValue() + "'");} } private static class Recolor extends BaseOperation<Integer> { public Recolor(int oValue) { super(oValue); } public void undo(TextArea TA) {System.out.println("Undo Recolor - Set it back to color " + getOldValue());} } private static class Resize extends BaseOperation<Integer> { public Resize(int oValue) { super(oValue); } public void undo(TextArea TA) {System.out.println("Undo Resize - Set it back to size " + getOldValue());} } private static class Italic extends BaseOperation<Boolean> { public Italic(Boolean oValue) { super(oValue); } public void undo(TextArea TA) {System.out.println("Undo Italic - Set it back to italic " + getOldValue()); } } UndoStack extends Stack (java)
14
Case Study: Undo 2 1 OUTPUT: Undo Italic - Set it back to italic false
private static class TextArea { // WHATEVER - NOT IMPORTANT FOR THE EXAMPLE } private static class UndoStack { Stack<Operation> stack=new Stack<>(); TextArea TA; public UndoStack(TextArea _TA) { TA = _TA; } public void add(Operation op) { stack.add(op); } public boolean undo() { if (stack.isEmpty()) return false; Operation op = stack.pop(); op.undo(TA); return true; } } 1 private static class UndoStack { public static void main(String[] args) { TextArea TA = new TextArea(); UndoStack ustack = new UndoStack(TA); ustack.add(new Text("")); //New value is "Hello World" ustack.add(new Resize(18)); //New value is ustack.add(new Recolor(255)); //New value is red ustack.add(new Italic(false)); //New value is True while (ustack.undo()) ; } } 2 UndoStack extends Stack (java) OUTPUT: Undo Italic - Set it back to italic false Undo Recolor - Set it back to color 255 Undo Resize - Set it back to size18 Undo Text - Set it back to text''
15
Maps An object that maps keys to values.
A map cannot contain duplicate keys. A map can contain duplicate values. each key can map to at most one value.
16
Maps 1 2 3 4 5 6 9 מערכים: מפות: 1 7 10 13 15 30 55 6 8 4 46 11 Index
1 2 3 4 5 6 9 Index מערכים: Value 1 7 10 13 15 30 55 6 8 4 46 11 Key מפות: Value
17
Words count Generates a frequency table of the words found in a sentence. Example: how much wood could a woodchuck chuck if a woodchuck could chuck wood. wood=2, could=2, how=1, if=1, chuck=2, a=2, woodchuck=2, much=1
18
Maps Words count Example
Generates a frequency table of the words found in a sentence. String sentence = "how much wood could a woodchuck chuck if a woodchuck could chuck wood"; Q: Who’s the key and Who’s the value? Map<String, Integer> map = new HashMap<>(); for (String word : sentence.split(" ")) { if (map.containsKey(word)) map.put(word, map.get(word) + 1); else map.put(word, 1); }
19
Maps Words count Example
Alternative code: String sentence = "how much wood could a woodchuck chuck if a woodchuck could chuck wood"; Map<String, Integer> map = new HashMap<>(); for (String word : sentence.split(" ")) { Integer freq = map.getOrDefault(word, 0); map.put(word, freq + 1); }
20
HashMap vs. TreeMap vs. LinkedHashMap
Iteration order "how much wood could a woodchuck chuck if a woodchuck could chuck wood"; HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added. TreeMap will iterate according to the "natural ordering" of the keys according to their compareTo() method (or an externally supplied Comparator). {a=2, chuck=2, could=2, how=1, if=1, much=1, wood=2, woodchuck=2} LinkedHashMap will iterate in the order in which the entries were put into the map. {how=1, much=1, wood=2, could=2, a=2, woodchuck=2, chuck=2, if=1}
21
HashMap vs. TreeMap vs. LinkedHashMap
Implement of the Map interface HashMap is a map based on hashing of the keys. Keys must have consistent implementations of hashCode() and equals() for this to work. In average it takes constant time for put/get) TreeMap is a tree based mapping. time. It requires items to have some comparison mechanism, either with Comparable or Comparator. The iteration order is determined by this mechanism. Its put/get operations take O(log n) LinkedHashMap is very similar to HashMap, but it adds awareness to the order at which items are added by maintains a doubly-linked list. it provides constant-time performance for the basic operations
22
Maps HashMap Example import java.util.HashMap; import java.util.Map.Entry; public class TestHashMap { public class Dog { String color; Dog(String c) { //Constructor color = c; } public String toString() { return color + " dog"; } } public static void main (String[] args) { HashMap<Dog, Integer> hashMap = new HashMap<>(); hashMap.put(new Dog("red"), 10); hashMap.put(new Dog("black"), 15); hashMap.put(new Dog("white"), 5); hashMap.put(new Dog("white"), 20); System.out.println(hashMap.size()); for (Entry<Dog, Integer> entry: hashMap.entrySet()) System.out.println(entry.getKey() + " - " + entry.getValue()); } } OUTPUT: 4 black dog - 15 white dog - 20 red dog - 10 white dog - 5
23
Maps HashMap Example import java.util.HashMap; import java.util.Map.Entry; public class TestHashMap { public class Dog { String color; Dog(String c) { //Constructor color = c; } public String toString() { return color + " dog"; } //Override the default Object’s equals function public boolean equals(Object o) { return ((Dog) o).color.equals(this.color); } } //What will be the output now ? public static void main (String[] args) { HashMap<Dog, Integer> hashMap = new HashMap<>(); hashMap.put(new Dog("red"), 10); hashMap.put(new Dog("black"), 15); hashMap.put(new Dog("white"), 5); hashMap.put(new Dog("white"), 20); System.out.println(hashMap.size()); for (Entry<Dog, Integer> entry: hashMap.entrySet()) System.out.println(entry.getKey() + " - " + entry.getValue()); } } OUTPUT: 3 red dog - 10 black dog - 15 white dog - 20
24
When using TreeMap The key object must implement Comparable<T>
Maps TreeMap Example import java.util.Map.Entry; import java.util.TreeMap; public class TestTreeMap { private static class Dog implements Comparable<Dog> { String color; int size; Dog(String c, int s) { //Constructor color=c; size=s; } public String toString() { return color + " dog, size " + size; } @Override public int compareTo(Dog other) { return (size - other.size); } } public static void main (String[] args) { TreeMap<Dog, Integer> treeMap = new TreeMap<>(); treeMap.put(new Dog("red", 333), 10); treeMap.put(new Dog("black", 222), 15); treeMap.put(new Dog("white", 555), 5); treeMap.put(new Dog("white", 444), 20); System.out.println(treeMap.size()); for (Entry<Dog, Integer> entry: treeMap.entrySet()) System.out.println(entry.getKey() + " - " + entry.getValue()); } } When using TreeMap The key object must implement Comparable<T> OUTPUT: 4 black dog, size red dog, size white dog, size white dog, size Iterate elements in increasing order (smallest to largest ) !!!
25
Maps TreeMap Example import java.util.Map.Entry; import java.util.TreeMap; public class TestTreeMap { private static class Dog implements Comparable<Dog> { String color; int size; Dog(String c, int s) { //Constructor color=c; size=s; } public String toString() { return color + " dog, size " + size; } @Override public int compareTo(Dog d) { return (size - d.size); } } public static void main (String[] args) { TreeMap<Dog, Integer> treeMap = new TreeMap<>(); treeMap.put(new Dog("red", 333), 10); treeMap.put(new Dog("black", 222), 15); treeMap.put(new Dog("white", 222), 5); treeMap.put(new Dog("white", 444), 20); System.out.println(treeMap.size()); for (Entry<Dog, Integer> entry: treeMap.entrySet()) System.out.println(entry.getKey() + " - " + entry.getValue()); } } //Changed the white dog size from 555 to 222 // What will be the output now ? OUTPUT: 3 black dog, size red dog, size white dog, size
26
The Collections class The 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);
27
A wrapper class is an object representing a primitive type.
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.
28
Wrapper Classes Integer Float Double Character Boolean int float
Example: Integer intObj = new Integer(10); int number = intObj.intValue();
29
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 )); for (Integer i: numVec) System.out.println( i); Collections.sort(numVec); 10 -> 1 1 -> 10
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.