Practical Session 4 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.
Collections CS3250. Sources  Slides by Professor Chuck Allison  Core Java, by Cay S. Horstmann and Gary Cornell  The Java Tutorial 
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.
12-Jul-15 Lists in Java Part of the Collections Framework.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
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.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
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.
Elementary Data Organization. Outline  Data, Entity and Information  Primitive data types  Non primitive data Types  Data structure  Definition 
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Java Collections CHAPTER 3-February-2003
תכנות מכוון עצמים ושפת JAVA
Slides by Donald W. Smith
Java Collections OOP tirgul No
Using the Java Collection Libraries COMP 103 # T2
Building Java Programs
Sixth Lecture ArrayList Abstract Class and Interface
Java's Collection Framework
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 Representation Methods
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Road Map CS Concepts Data Structures Java Language Java Collections
Welcome to CSE 143!.
Java Collections Overview
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
Introduction to Collections
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Java Collections Framework
Chapter 10 ArrayList reading: 10.1
Welcome to CSE 143!.
Collections in Java The objectives of this lecture are:
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.
CS2110: Software Development Methods
Introduction to Collections
Collections Framework
CSE 1020: The Collection Framework
Introduction to Collections
Containers and Iterators
slides created by Marty Stepp
Part of the Collections Framework
CS 240 – Advanced Programming Concepts
Practical Session 3 Java Collections
Presentation transcript:

Practical Session 4 Java Collections

Outline Working with a Collection The Collection interface The Collection hierarchy Maps Case Study: Undoable Stack 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); }

Map types HashMap<K,V>. This implementation provides constant-time performance for the basic operations (get and put). Uses the hashCode() method. String sentence = "how much wood could a woodchuck chuck if a woodchuck could chuck wood"; Map<String, Integer> map = new HashMap<String, Integer>(); {wood=2, could=2, how=1, if=1, chuck=2, a=2, woodchuck=2, much=1}

Map types TreeMap<K,V>. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time. This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. String sentence = "how much wood could a woodchuck chuck if a woodchuck could chuck wood"; Map<String, Integer> map = new TreeMap<String, Integer>(); {a=2, chuck=2, could=2, how=1, if=1, much=1, wood=2, woodchuck=2}

Map types LinkedHashMap<K,V>. A HashMap that maintains a doubly-linked list. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). String sentence = "how much wood could a woodchuck chuck if a woodchuck could chuck wood"; Map<String, Integer> map = new LinkedHashMap<String, Integer>(); {how=1, much=1, wood=2, could=2, a=2, woodchuck=2, chuck=2, if=1}

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