Practical Session 3 Java Collections

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Chapter 19 Java Data Structures
Java's Collection Framework
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Unit 10 - Data Structures Objectives 1. Describe the usage of data structures. 2. Develop simple data structures. 3. Apply Java collections. 4. Apply.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Data structures and algorithms in the collection framework 1.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Practical Session 4 Java Collections. Outline Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack The.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Java Collections CHAPTER 3-February-2003
Java Collections OOP tirgul No
Using the Java Collection Libraries COMP 103 # T2
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 19 Java Data Structures
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
Software Development Java Collections
University of Central Florida COP 3330 Object Oriented Programming
Data Structures TreeMap HashMap.
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Road Map CS Concepts Data Structures Java Language Java Collections
Java Collections Overview
TreeSet TreeMap HashMap
Introduction to Collections
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
JAVA Collections Framework Set Interfaces & Implementations
Practical Session 3 Java Collections
Introduction to Collections
Practical Session 4 Java Collections
Collections in Java The objectives of this lecture are:
Introduction to Collections
Collections Framework
CSE 1020: The Collection Framework
Introduction to Collections
Part of the Collections Framework
Presentation transcript:

Practical Session 3 Java Collections

Outline Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack Maps The Collections class Wrapper classes

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…

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!

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);

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

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

The Collection Hierarchy (Partial illustration) Collection Iterable List Set Queue Vector LinkedList PriorityQueue Stack

The Collection Hierarchy

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

Case Study: Undo Stack Action Type “hello world” Resize 44 Recolor red Italic on Hello World

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)

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)

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 44 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''

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.

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

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

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); }

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); }

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}

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

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

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

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 222 - 15 red dog, size 333 - 10 white dog, size 444 - 20 white dog, size 555 - 5 Iterate elements in increasing order (smallest to largest ) !!!

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 222 - 5 red dog, size 333 - 10 white dog, size 444 - 20

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);

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.

Wrapper Classes Integer Float Double Character Boolean int float Example: Integer intObj = new Integer(10); int number = intObj.intValue();

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