CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

Similar documents
The Java Collections Framework. Chapters 7.5

Lecture 4. The Java Collections Framework

C27a: Stack and Queue

EECS 2011 M: Fundamentals of Data Structures

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

Notes on access restrictions

JAVA.UTIL.LINKEDLIST CLASS

To describe the Java Collections Framework. To use the Iterator interface to traverse a. To discover the Set interface, and know how

C12a: The Object Superclass and Selected Methods

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

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

CS/CE 2336 Computer Science II

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

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

An Introduction to Data Structures

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

Stacks and Queues. David Greenstein Monta Vista

Lists. The List ADT. Reading: Textbook Sections

Functors. 01/23/03 Lecture 5 1

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

Generics. IRS W-9 Form

Iterators and Sequences

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

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

Outline. iterator review iterator implementation the Java foreach statement testing

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/22/04 Lecture 4 1

Frameworks. CS151 Chris Pollett Oct. 26, 2005.

ITI Introduction to Computing II

Java Review: Objects

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

Implementing Lists, Stacks, Queues, and Priority Queues

Overview of Java ArrayList, HashTable, HashMap, Hashet,LinkedList

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/30/03 Lecture 7 1

CISC 3115 TY3. C21a: Recursion. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/6/2018 CUNY Brooklyn College

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

Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Queue Implemented with a CircularArray. Thursday, April 25, 13

Lecture 9: Lists. MIT-AITI Kenya 2005

Java Collection Framework

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

package weiss.util; // Fig , pg // Fig 6.16,6.17, pg package weiss.util;

Doubly LinkedList is Symmetrical! LinkedList Efficiency. Monday, April 8, 13. insert insert remove remove remove walk

Java Collections Framework: Interfaces

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

ArrayList. Introduction. java.util.arraylist

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

Appendix A: Interfaces and Classes in the AP Java Subset (AB)

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

C20a: Selected Interfaces in Java API

Collections (Java) Collections Framework

C18a: Abstract Class and Method

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

C22a: Problem Solving using Recursion

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

Advanced Java Concepts Unit 2: Linked Lists.

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Basic Data Structures 1 / 24

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

Lecture 12: Doubly Linked Lists

What is the Java Collections Framework?

C02: Overview of Software Development and Java

C09: Interface, and Abstract Class and Method

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

CS350: Data Structures. Doubly Linked Lists

Java Collections. Wrapper classes. Wrapper classes

Java Collections. Engi Hafez Seliem

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Basic Data Structures

+ Abstract Data Types

Linked List Nodes (reminder)

An Interface with Generics

C30b: Inner Class, Anonymous Class, and Lambda Expression

Today: Java Library Classes for lists. Iterators, ListIterators. CS61B Lecture #7. Last modified: Fri Sep 12 14:41: CS61B: Lecture #7 1

C11: Garbage Collection and Constructors

Class 26: Linked Lists

COMP-202. Generics. COMP Generics, 2013 Jörg Kienzle and others

CSE 143. Computer Programming II

Case Study: Debugging a Discovered Specification for java.util.arraylist by Using Algebraic Interpretation

CSC212:Data Structure

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

JAVA COLLECTION FRAMEWORK & SETS

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

C06: Java API & Libraries

C09: Interface and Abstract Class and Method

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

Lecture 6: ArrayList Implementation

CS 302: Introduction to Programming in Java. Lecture 12

Algorithms. Algorithms 1.3 STACKS AND QUEUES. stacks resizing arrays queues generics iterators applications ROBERT SEDGEWICK KEVIN WAYNE.

CS 307 Final Spring 2008

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Active Learning: Streams

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

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

Transcription:

CISC 3115 TY3 C24a: Lists Hui Chen Department of Computer & Information Science CUNY Brooklyn College 11/15/2018 CUNY Brooklyn College 1

Outline Concept of data structure Use data structures List Stack Queue and priority queue Set and map 11/15/2018 CUNY Brooklyn College 2

Outline of This Lecture Data structure and Java Collections Concept of data structure and Java Collection Framework Type hierarchy of Java Collection Framework The Collection interface List, ArrayList, and LinkedList 11/15/2018 CUNY Brooklyn College 3

Data Structure A collection of data organized in some fashion in a program. Data elements Operations for accessing and manipulating the data elements In Java, how do we represent data and operations? Data: primitive data type variables; objects and reference variables Operations: methods 11/15/2018 CUNY Brooklyn College 4

Java Collection Framework A collection is an object that represents a group of objects Essentially, a collection is a representation/an implementation of a data structure Java Collection Framework A unified architecture for representing and manipulating collections, enabling collections to be manipulated independently of implementation details. Data structures: list, stack, and queue 11/15/2018 CUNY Brooklyn College 5

Java Collection Framework Hierarchy 11/15/2018 CUNY Brooklyn College 6

The Collection Interface 11/15/2018 CUNY Brooklyn College 7

«interface» java.lang.iterable<e> +iterator(): Iterator<E> +foreach(action: Consumer<? super E>): default void Returns an iterator for the elements in this collection. Performs an action for each element in this iterator. «interface» java.util.collection<e> +add(e: E): boolean +addall(c: Collection<? extends E>): boolean +clear(): void +contains(o: Object): boolean +containsall(c: Collection<?>):boolean +isempty(): boolean +remove(o: Object): boolean +removeall(c: Collection<?>): boolean +retainall(c: Collection<?>): boolean +size(): int +toarray(): Object[] +stream(): Stream default +parallelstream(): Stream default Adds a new element e to this collection. Adds all the elements in the collection c to this collection. Removes all the elements from this collection. Returns true if this collection contains the element o. Returns true if this collection contains all the elements in c. Returns true if this collection contains no elements. Removes the element o from this collection. Removes all the elements in c from this collection. Retains the elements that are both in c and in this collection. Returns the number of elements in this collection. Returns an array of Object for the elements in this collection. Returns a stream from this collection (covered in Ch 23). Returns a parallel stream from this collection (covered in Ch 23). «interface» java.util.iterator<e> +hasnext(): boolean +next(): E +remove(): void Returns true if this iterator has more elements to traverse. Returns the next element from this iterator. Removes the last element obtained using the next method. 11/15/2018 CUNY Brooklyn College 8

Questions? How about we take a look at Java API documentation about these? Concept of data structure Concept of Java Collection Framework Relationship between data structure and Java Collection Type Hierarchy of Java Collection Framework 11/15/2018 CUNY Brooklyn College 9

The List Interface 11/15/2018 CUNY Brooklyn College 10

The List Data Structure A list stores elements in a sequential order, and allows the user to specify where the element is stored. The user may be able to access the elements by index. However, in general, one should not assume that it takes equal amount of time to access different elements using their indices 11/15/2018 CUNY Brooklyn College 11

The List Interface 11/15/2018 CUNY Brooklyn College 12

ListIterator? 11/15/2018 CUNY Brooklyn College 13

Recall: The Collection Interface «interface» java.lang.iterable<e> +iterator(): Iterator<E> +foreach(action: Consumer<? super E>): default void Returns an iterator for the elements in this collection. Performs an action for each element in this iterator. «interface» java.util.collection<e> +add(e: E): boolean +addall(c: Collection<? extends E>): boolean +clear(): void +contains(o: Object): boolean +containsall(c: Collection<?>):boolean +isempty(): boolean +remove(o: Object): boolean +removeall(c: Collection<?>): boolean +retainall(c: Collection<?>): boolean +size(): int +toarray(): Object[] +stream(): Stream default «interface» java.util.iterator<e> +hasnext(): boolean +next(): E +remove(): void Adds a new element e to this collection. Adds all the elements in the collection c to this collection. Removes all the elements from this collection. Returns true if this collection contains the element o. Returns true if this collection contains all the elements in c. Returns true if this collection contains no elements. Removes the element o from this collection. Removes all the elements in c from this collection. Retains the elements that are both in c and in this collection. Returns the number of elements in this collection. Returns an array of Object for the elements in this collection. Returns a stream from this collection (covered in Ch 23). Returns a parallel stream from this collection (covered in Ch 23). Returns true if this iterator has more elements to traverse. Returns the next element from this iterator. Removes the last element obtained using the next method. 11/15/2018 CUNY Brooklyn College 14

The ListIterator 11/15/2018 CUNY Brooklyn College 15

Questions? The List interface Methods and type hierarchy How much do you remember? 11/15/2018 CUNY Brooklyn College 16

Lists: ArrayList and LinkedList 11/15/2018 CUNY Brooklyn College 17

ArrayList and LinkedList List stores elements in a sequential order, allows the user to specify where the element is stored. the user may be able to access the elements by index. ArrayList Efficient random access: access it like an array: access any element at (almost) equal amount of time and efficiently (called near-constant-time positional access) In general, costly to remove and insert elements LinkedList Efficient to add and remove elements anywhere Costly random access (does not provide near-constant-time positional access) 11/15/2018 CUNY Brooklyn College 18

Array, ArrayList, and LinkedList: When to Use? ArrayList: if your application needs to support random access through an index without inserting or removing elements from any place other than the end LinkedList: if your application requires the insertion or deletion of elements from any place in the list Array: an array is fixed in size while a list can grow or shrink dynamically. If your application knows the size and does not require insertion or deletion of elements, the most efficient data structure is the array. 11/15/2018 CUNY Brooklyn College 19

Questions? Concept and type hierarchy of ArrayList and Linked List When to use array, ArrayList, and LinkedList? 11/15/2018 CUNY Brooklyn College 20

The ArrayList 11/15/2018 CUNY Brooklyn College 21

ArrayList: java.util.arraylist «interface» java.util.collection<e> «interface» java.util.list<e> java.util.arraylist<e> +ArrayList() +ArrayList(c: Collection<? extends E>) +ArrayList(initialCapacity: int) +trimtosize(): void Creates an empty list with the default initial capacity. Creates an array list from an existing collection. Creates an empty list with the specified initial capacity. Trims the capacity of this ArrayList instance to be the list's current size. 11/15/2018 CUNY Brooklyn College 22

The LinkedList 11/15/2018 CUNY Brooklyn College 23

LinkedList: java.util.linkedlist «interface» java.util.collection<e> «interface» java.util.list<e> java.util.linkedlist<e> +LinkedList() +LinkedList(c: Collection<? extends E>) +addfirst(o: E): void +addlast(o: E): void +getfirst(): E +getlast(): E +removefirst(): E +removelast(): E Creates a default empty linked list. Creates a linked list from an existing collection. Adds the object to the head of this list. Adds the object to the tail of this list. Returns the first element from this list. Returns the last element from this list. Returns and removes the first element from this list. Returns and removes the last element from this list. 11/15/2018 CUNY Brooklyn College 24

Explore ArrayList and LinkedList: Examples Random access cost Insertion and deletion cost Iterating lists Updating objects in lists 11/15/2018 CUNY Brooklyn College 25

Questions? Use ArrayList and LinkedList Access: random and sequential Use loops and iterators Update objects in lists 11/15/2018 CUNY Brooklyn College 26