COMP6700/2140 Abstract Data Types: Queue, Set, Map

Similar documents
COMP6700/2140 Implementation of ADT

Class 32: The Java Collections Framework

What is the Java Collections Framework?

Sets and Maps. Part of the Collections Framework

Type Parameters: E - the type of elements returned by this iterator Methods Modifier and Type Method and Description

Collections (Java) Collections Framework

CSC 1214: Object-Oriented Programming

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

CONTAİNERS COLLECTİONS

Lecture 6 Collections

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University

COMP6700/2140 Abstract Data Types: Lists and Iteration

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

Java Collections Framework. 24 April 2013 OSU CSE 1

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

COMP6700/2140 Operators, Expressions, Statements

Generics Collection Framework

Abstract data types (again) Announcements. Example ADT an integer bag (next) The Java Collections Framework

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

An Interface with Generics

182 review 1. Course Goals

Important Dates. Game State and Tree. Today s topics. Game Tree and Mini-Max. Games and Mini-Max 3/20/14

Collections (Collection Framework) Sang Shin Java Technology Architect Sun Microsystems, Inc.

Pieter van den Hombergh Richard van den Ham. February 8, 2018

DM550 Introduction to Programming part 2. Jan Baumbach.

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList!

Java collections framework

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

Algorithms. Produced by. Eamonn de Leastar

COMP6700/2140 Recursive Operations

Java collections framework

Generics and collections

COMP6700/2140 Methods

36. Collections. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Pop Quiz 2: Answers. Data structures Recursion Big-O

Standard ADTs. Lecture 19 CS2110 Summer 2009

Recap. List Types. List Functionality. ListIterator. Adapter Design Pattern. Department of Computer Science 1

[Ref: Core Java Chp 13, Intro to Java Programming [Liang] Chp 22, Absolute Java Chp 16, docs.oracle.com/javase/tutorial/collections/toc.

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. June 8, 2017

COMP6700/2140 Data and Types

Arrays organize each data element as sequential memory cells each accessed by an index. data data data data data data data data

COMP6700/2140 Std. Lib., I/O

Collections Framework: Part 2

Java Collections Framework reloaded

27/04/2012. Objectives. Collection. Collections Framework. "Collection" Interface. Collection algorithm. Legacy collection

DM550 Introduction to Programming part 2. Jan Baumbach.

Topic 10: The Java Collections Framework (and Iterators)

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

Abstract Data Types Data Structure Grand Tour Java Collections.

Abstract Data Types Data Structure Grand Tour Java Collections.

Java Magistère BFA

CS2113 Lab: Collections 10/29/2018

COMP6700/2140 Sorting and Searching

Computer Science II (Spring )

Java Collections Framework

Abstract Data Types Data Structure Grand Tour Java Collections.

COMP6700/2140 Control Flow

Abstract Data Types Spring 2018 Exam Prep 5: February 12, 2018

CS2110: Software Development Methods. Maps and Sets in Java

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

COMP6700/2140 Generic Methods

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

COMP 250 Midterm #2 March 11 th 2013

Implementation. (Mapping to Java) Jörg Kienzle & Alfred Strohmeier. COMP-533 Implementation

Software 1 with Java. Recitation No. 6 (Collections)

School of Computing and Information Sciences. Course Title: Data Structures Date: 3/30/2010 Course Number: COP 3530 Number of Credits: 3

CSE 373. Java Collection Framework. reading: Weiss Ch. 3, 4.8. slides created by Marty Stepp

Collections. James Brucker

Model Solutions. COMP 103: Test April, 2013

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

COMP6700/2140 Packages, Modules and Jigsaw

Check out from SVN: ComparatorExample project

HEAPS & PRIORITY QUEUES

Introduction to Collections

COMP6700/2140 Code as Data

PIC 20A Collections and Data Structures

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 PRACTICE EXAM

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

CS 310: Maps and Sets

Lecture 15 Summary 3/11/2009. By the end of this lecture, you will be able to use different types of Collections and Maps in your Java code.

CISC 3115 TY3. C28a: Set. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/29/2018 CUNY Brooklyn College

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Software 1 with Java. Java Collections Framework. Collection Interfaces. Online Resources. The Collection Interface

Java Data Structures Collections Framework BY ASIF AHMED CSI-211 (OBJECT ORIENTED PROGRAMMING)

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

COMP6700/2140 Correctness and Efficiency

Generic classes & the Java Collections Framework. *Really* Reusable Code

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

CSC212:Data Structure

9/16/2010 CS Ananda Gunawardena

Framework in Java 5. DAAD project Joint Course on OOP using Java

COMP6700/2140 GUI and Event Driven Programming

CS Ananda Gunawardena

Object-Oriented Programming with Java

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Collections & Maps. Lecture 12. Collections & Maps

CS 146 Spring 2017 Homework 4 Sections 5 and 6. The homework assignment is available at:

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Transcription:

COMP6700/2140 Abstract Data Types: Queue, Set, Map Alexei B Khorev and Josh Milthorpe Research School of Computer Science, ANU 19 April 2017 Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Abstract Data Types: Queue, Set, Map 19 April 2017 1 / 10

Java Collection Interfaces The Java Collections Framework (JCF) includes interfaces (ADTs), implementations (data structures), and algorithms The main interfaces are: Iterable most basic interface to use a data type for iteration and nothing else Collection (proper) allows adding, removing and testing for elements List collection whose elements are ordered and accessible by their location in the collection Set a collection with no duplicates SortedSet an ordered collection which contains no duplicates Queue a collection where elements are removed according to some order (typically first-in, first-out (FIFO)) Deque a double-ended queue Map a collection where elements are stored and retrieved not by an index, but via a key Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Abstract Data Types: Queue, Set, Map 19 April 2017 2 / 10

Collections: Class Diagram Key interfaces of the Java Collections Framework and some implementations: Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Abstract Data Types: Queue, Set, Map 19 April 2017 3 / 10

Queues A Queue is a first-in, first-out (FIFO) type which can be implemented by adding objects to the head of a list and removing them from its tail The interface: The JCF Queue interface has slightly different names for the above operations, as well as allowing different orderings (defined by the constructor with Comparator parameter), for instance, in the PriorityQueue class Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Abstract Data Types: Queue, Set, Map 19 April 2017 4 / 10

Stacks Queues and stacks are widely used in systems level programming (for managing memory and processes) and other applications A Stack is a last-in, first-out (LIFO) collection type which can be implemented by adding to and removing from the head of a list The interface: There is a legacy Stack class in JCF, however, the recommended implementation is the Deque class Deque implements addfirst, removefirst and peekfirst methods Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Abstract Data Types: Queue, Set, Map 19 April 2017 5 / 10

Set and SortedSet The extensions of Collection type which disallow duplicates (identical elements): a repeated invocation of add(elem) with the same element elem (or such elem1, that elemequals(elem1) returns true) returns false, and the collection remains unchanged Set types, therefore, are sets in the mathematical sense (some computer scientists call them bags ) The elements of a set are unordered The subtype SortedSet (extension of the Set interface) represents a collection, whose elements can be compared they implement Comparable interface The ordering allows to introduce additional methods: Set is implemented by the HashSet class with a hash table as the DS Content modification (add, set) and testing (contains) are O(1) operations SortedSet is implemented by the TreeSet class with a binary tree as the DS If the implementation can maintain the balanced tree, the search and modify operations are O(log 2 N) Example SetTestjava Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Abstract Data Types: Queue, Set, Map 19 April 2017 6 / 10

Map and SortedMap Unlike all previous types from JCF, the Map family of types does not extends the Collection interface: their contract is different to represent not a collection of elements, but a correspondence between two collections So, a map contains key/value pairs, with no duplicate keys (the keys form a set) and at most one value for each key Map is a model of a mathematical abstraction called function The interface Map<K,V> operations are: The SortedMap extension requires the set of keys be sorted Methods like firstkey() and lastkey() are added The Map interface has two major implementation classes (similar to Set) HashMap which uses a hash table DS for the implementation (with similar O(1) performance for put/get operations), and TreeMap which implements SortedMap in the similar to TreeSet way (with O(log 2 N) efficiency) Example MapTestjava Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Abstract Data Types: Queue, Set, Map 19 April 2017 7 / 10

Bulk operations of Collection interface The basic Collection operations were listed above (slide Collection Interface) They allow to examine and manipulate the collection element by element The bulk operations allow to manipulate the whole part of the collection in one go: boolean containsall(collection<?> c); boolean addall(collection<? extends E> c); adds all elements from c to this boolean retainall(collection<?> c); retains only those elements that are found in c boolean removeall(collection<?> c); opposite to retainall() List<E> sublist(int, int) like a Stringsubstring(int,int), figure out the rest void clear(); removes all elements ( clean start ) Another category of operations are array operations: Object[ ] toarray(); returns an array of all the elements in this list in the correct order <T> T[ ] toarray(t[ ] dest); the list elements are placed in the array dest, which is returned (if dest isn t big enough to accommodate all elements of the collection, a new, properly sized array is created and returned) Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Abstract Data Types: Queue, Set, Map 19 April 2017 8 / 10

How to copy arrays properly Arrays are objects, therefore when one array is assigned to another, the two identifiers point to the same memory location How to copy elements of an array into a different array? To achieve element-by-element copying, one has to use the Systemarraycopy()method: To copy count elements from source array starting with index from to target array beginning with index to, make the following call: Systemarraycopy(source,from,target,to,count); int[] smallprimes = {2,3,5,7,11,13}; int[] luckynumbers = {1001,1002,1003,1004,1005,1006,1007}; Systemarraycopy(smallPrimes,2,luckyNumbers,3,4); for (int i=0; i<luckynumberslength; i++) Systemoutprintln(i + ":" + luckynumbers[i]); Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Abstract Data Types: Queue, Set, Map 19 April 2017 9 / 10

Further Reading Hortsmann Core Java for the Impatient, Ch 73 75 Oracle The Java Tutorials: Collections (Sections 1-3) Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Abstract Data Types: Queue, Set, Map 19 April 2017 10 / 10