Practical Session 4 Java Collections. Outline Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack The.

Slides:



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

The ArrayList Class and the enum Keyword
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
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 
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
Professor Evan Korth (adapted from Sun’s collections documentation)
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 1 Topic 12 ADTS, Data Structures, Java Collections and Generic Data Structures.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Java Collections Package C. DeJong Fall 2001.
Chapter 19 Java Data Structures
Building Java Programs
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Computer Science 209 Software Development Java Collections.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 Generics Chapter 21 Liang, Introduction to Java Programming.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Elementary Data Organization. Outline  Data, Entity and Information  Primitive data types  Non primitive data Types  Data structure  Definition 
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
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.
Java Collections OOP tirgul No
Sixth Lecture ArrayList Abstract Class and Interface
Software Development Java Collections
Building Java Programs
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Welcome to CSE 143!.
TCSS 143, Autumn 2004 Lecture Notes
JAVA Collections Framework Set Interfaces & Implementations
Unit-2 Objects and Classes
Practical Session 3 Java 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.
Practical Session 4 Java Collections
Welcome to CSE 143!.
ArrayLists 22-Feb-19.
Collections Framework
slides created by Alyssa Harding
Practical Session 3 Java Collections
Presentation transcript:

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. 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,…

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 import java.util.Vector; … Vector carsVec = new Vector (); 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 colName = new Collection () Example:

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:

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 extends Iterable { int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator iterator(); Object[] toArray(); T[] toArray(T[] a); …

The Collection Hierarchy Collection ListSetQueue (Partial illustration)

The Collection Hierarchy Collection ListSetQueue Vector (Partial illustration)

The Collection Hierarchy Collection ListSetQueue Vector Stack ArrayListPriorityQueue (Partial illustration)

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

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

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

Undoable Stack Hierarchy

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

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

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…

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

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 carsVec = new Vector (); … Collections.reverse(carsVec);

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 int float double char boolean Integer Float Double Character Boolean Example: Integer intObj = new Integer(10); int number = intObj.intValue();

A collection of wrapper objects Import java.util.Vector; … Vector numVec = new Vector (); 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);