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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 19 Java Data Structures
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
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.
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.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
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.
Data structures Abstract data types Java classes for Data structures and ADTs.
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.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
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.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
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.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Java Collections CHAPTER 3-February-2003
Slides by Donald W. Smith
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
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Road Map CS Concepts Data Structures Java Language Java Collections
Java Collections Overview
Introduction to Collections
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
JAVA Collections Framework Set Interfaces & Implementations
Introduction to Collections
Part of the Collections Framework
Java Collections Framework
Practical Session 4 Java Collections
Collections in Java The objectives of this lecture are:
Dynamic Data Structures and Generics
Welcome to CSE 143! Go to pollev.com/cse143.
Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection.
Introduction to Collections
Collections Framework
CSE 1020: The Collection Framework
Introduction to Collections
slides created by Marty Stepp
Part of the Collections Framework
Practical Session 3 Java Collections
Presentation transcript:

Practical Session 3 Java Collections

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

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, ArrayList, Stack, ArrayDequeue, PriorityQueue, TreeSet, HashMap…

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 …

Working with a collection Definition syntax: Collection <type> colName = new Collection <type>() Example: 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);

Working with a collection Example - continued: 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));

The Collection Interface Collection 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); …

The Collection Hierarchy (Partial illustration) Collection List Set Queue

The Collection Hierarchy (Partial illustration) Collection List Set Queue Vector

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

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.

Program Stack

Program Stack Resize 36

Program Stack Resize 36 Recolor 4 Resize 36

Program Stack Resize 24 Recolor 4 Resize 36 Undo: Resize(36)

Program Stack Undo: Resize(36) Resize 24 Recolor 4 Resize 36 Recolor 4

Undoable Stack Hierarchy

Undoable Stack Hierarchy TextArea -text: String -size: int -color: int + getters… + setters…

Undoable Stack Hierarchy TextArea -text: String -size: int -color: int + getters… + setters… Operation + perform(arg: int) + getArg() : int

Undoable Stack Hierarchy TextArea -text: String -size: int -color: int + getters… + setters… Operation + perform(arg: int) + getArg() : int Recolor - color: int + perform(arg: int) + getArg() : int Resize - size: int + perform(arg: int) + getArg() : int

Undoable Stack Hierarchy TextArea -text: String -size: int -color: int + getters… + setters… Operation + perform(arg: int) + getArg() : int 0..n Recolor - color: int + perform(arg: int) + getArg() : int Resize - size: int + perform(arg: int) + getArg() : int UndoStack + add (op :Operation) + undo()

Maps An object that maps keys to values. A map cannot contain duplicate keys. 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 מפות:

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

Words count String sentence = "how much wood could a woodchuck chuck if a woodchuck could chuck wood"; Map<String, Integer> map = new HashMap<String, Integer>(); for (String word : sentence.split(" ")) { Integer freq = map.get(word); map.put(word, (freq == null) ? 1 : freq + 1); }

HashMap vs. TreeMap vs. LinkedHashMap Iteration order String sentence = "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. TreeMap is a tree based mapping. Its put/get operations take O(log n) time. It requires items to have some comparison mechanism, either with Comparable or Comparator. The iteration order is determined by this mechanism. LinkedHashMap is very similar to HashMap, but it adds awareness to the order at which items are added (insertion order). It’s like a hashMap that maintains a doubly-linked list.

HashMap example (1) Output: white dog - 5 black dog - 15 red dog - 10

HashMap example (2) Output: 3 red dog - 10 white dog - 20 black dog - 15

TreeMap example (1) java_string_compareto (“Read & Focus on the return value”)  Iterate elements in increasing order (smallest to largest ) Output: white dog - 20 black dog - 15 red dog - 10 If "Dog d4 = new Dog("white", 10);" is replaced with "Dog d4 = new Dog("white", 40);“ the output would be: white dog - 5 black dog - 15 red dog - 10 white dog - 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 )); Collections.sort(numVec); for (Integer i: numVec) System.out.println( i);