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

Size: px
Start display at page:

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

Transcription

1 Chapter 22 Java Collections Framework :Objectives To describe the Java Collections Framework hierarchy To use the Iterator interface to traverse a collection To discover the Set interface, and know how and when to use HashSet to store elements To explore the List interface, LinkedList and Vector to store elements To explore Map, and know how to use HashMap to store values associated with keys 1

2 Java Collection Framework hierarchyh A collection is a container object that represents a group of objects, often referred to as elements. The Java Collections Framework supports three types of collections, named sets, lists, and maps. 2

3 Java Collection Framework hierarchy, h cont. Set and List are subinterfaces of Collection. SortedSet TreeSet Set AbstractSet HashSet LinkedHashSet Collection AbstractCollection Vector Stack List AbstractList ArrayList AbstractSequentialList LinkedList Interfaces Abstract Classes Concrete Classes Slide 5 3

4 Java Collection Framework hierarchy, h cont. An instance of Map represents a group of objects, each of which is associated with a key. You can get the object from a map using a key, and you have to use a key to put the object into the map. SortedMap TreeMap Map AbstractMap HashMap LinkedHashMap Interfaces Abstract Classes Concrete Classes 4

5 The Collection Interface:Set, List «interface» java.util.collection<e> +add(o: E): boolean +addall(c: Collection<? extends E): boolean +clear(): void +contains(o: Object): boolean +containsall(c: Collection<?>):boolean l +equals(o: Object): boolean +hashcode(): int +isempty(): boolean +iterator(): Iterator +remove(o: Object): boolean +removeall(c: Collection<?>): boolean +retainall(c: Collection<?>): boolean +size(): int +toarray(): Object[] The Collection interface is the root interface for manipulating a collection of objects. Adds a new element o 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 is equal to another collection o. Returns the hash code for this collection. Returns true if this collection contains no elements. Returns an iterator for the elements in this collection. 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. ReturnsanarrayofObjectfortheelementsinthiscollection array for the elements this collection. «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. slide3 5

6 The Set Interface The Set interface extends the Collection interface. It does not introduce new methods or ก าหนดเง อนไข constants, but it stipulates that an instance of Set contains no duplicate elements. That is no two elements e1 and e2 can be in the set such that e1.equals(e2) equals(e2) istrue. false Class HashSet implements this interface ค าข อม ลของสมาช กไม สามารถซ าก นได และล าด บของสมาช กไม ม ความส าค ญ 6

7 The Set Interface Hierarchy «interface» java.util.set<e> java.util.abstractset<e> java.util.hashset<e> +HashSet() +HashSet(c: Collection<? extends E>) java.util.linkedhashset<e> +LinkedHashSet() +LinkedHashSet(c: Collection<? extends E>) «interface» java.util.sortedset<e> +first(): E +last(): E +headset(toelement: E): SortedSet<E> +tailset(fromelement: E): SortedSet<E> java.util.treeset<e> +TreeSet() +TreeSet(c: Collection<? extends E>) +TreeSet(c: Comparator<? super E>) Returns the first in this set. Returns the last in this set. headset/tailset returns a portion of the set less than toelement/greater than fromelement. Creates a tree set with the specified comparator. 7

8 Example: HashSet 1 import java.util.*; 2 3 public class TestHashSet { 4 public static void main(string[] args) { 5 // Create a hash set 6 Set<String> set = new HashSet<>(); 7 set.add("london"); 8 set.add("new York"); 9 set.add("san Francisco"); 10 set.add("new York"); 11 System.out.println(set); 12 System.out.println("Before remove the size is:"+ set.size()); 13 set.remove("london"); } } ผลการร น ผลการรน System.out.println("set contain London:"+ set.contains("london")); System.out.println("After remove the size is: " + set.size()); [San Francisco, New York, London] Before remove the size is: 3 set contain London: false After remove the size is: 2 8

9 Iterator Interface ภายในอ นเตอร เฟส ภายในอนเตอรเฟส Collection ม เมธอดช อ มเมธอดชอ iterator() ส งค ากล บเป น Iterator ซ งเป นอ นเตอร เฟสส าหร บอ างถ งข อม ล สมาช กประเภท สมาชกประเภท Set ม เมธอดส าค ญด งน มเมธอดสาคญดงน boolean hasnext() Object t next() void remove() 9

10 Example:iterator() 1 import java.util.*; 2 3 public class TestHashSetIterator { 4 public static ti void main(string[] i args) { set.add("new York"); // Create a hash set Set<String> set = new HashSet<>(); set.add("london"); 9 set.add("san Francisco"); 10 // Obtain an iterator for the hash set 11 Iterator iterator = set.iterator(); 12 // Display the elements in the hash set } 17 } while (iterator.hasnext()) { System.out.print(iterator.next() + ", "); } output San Francisco, New York, London, 10

11 The List Interface The List interface extends the Collection interface. To allow duplicate elements to be stored in a collection, you need to use a list. Ali list can store duplicate elements, and also allow the user to specify where the element is stored. The user can access the element by index. ArrayList, Vector and LinkedList implement this interface คาข อม ลของสมาชกซ ากนได และล าดบของสมาชกม ความส าคญ 11

12 The List Interface, cont. Collection List +add(index: int, element: Object) : boolean +addall(index: int, collection: Collection) : boolean +get(index: int) : Object +indexof(element: Object) : int +lastindexof(element: Object) : int +listiterator() : ListIterator +listiterator(startindex: int) : ListIterator +remove(index: int) : int +set(index: int, element: Object) : Object +sublist(fromindex: int, toindex: int) : List Adds a new element at the specified index Adds all elements in the collection to this list at the specified index Returns the element in this list at the specified index Returns the index of the first matching element Returns the index of the last matching element Returns the list iterator for the elements in this list Returns the iterator for the elements from startindex Removes the element at the specified index Sets the element at the specified index Returns a sublist from fromindex to toindex 12

13 LinkedList List AbstractSequentialList LinkedList +addfirst(o: Object) : void +addlast(o: Object) : void +getfirst() : Object +getlast() : Object +removefirst() : Object +removelast() : Object 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 13

14 Example: LinkedList 1 import java.util.*; 2 3 public class TestList { 4 public static void main(string[] args) { 5 List<String> list = new LinkedList<>(); 6 list.add("america"); 7 list.add("canada"); 8 System.out.println("fist one: " + list.get(0)); 9 list.add(0,"russia"); 10 System.out.println("list: " + list); 11 System.out.println("Size: " + list.size()); 12 list.add("france"); 13 System.out.println("list: li " + list); 14 System.out.println("index of America is:"+list.indexof("america")); 15 } 16 } 17 output fist one: America list: [Russia, America, Canada] Size: 3 list: [Russia, America, Canada, France] index of America is:1 14

15 อ นเตอร เฟส ListIterator Iterator ListIterator +add(o: Object) : void +hasprevious() : boolean +nextindex() : int +previousindex() : int +previous() : Object +set(o: Object) : void +hasnext(): boolean +next(): E ใช อ างข อม ลสมาช กประเภท List ภายในอ นเตอร เฟส List ม เมธอดช อ listiterator() เป น เมธอดส งค ากล บเป น ListIterator Adds the specified object to the list Returns true if this list iterator has more elements when traversing backward. Returns the index of the next element Returns the index of the previosu element Returns the previous element in this list iterator Replaces the last element returned by the previous or next method with the specified element Returns true if this iterator has more elements to traverse. Returns the next element from this iterator 15

16 Example:ListIterator 1 import java.util.*; 2 public class TestListIterator { 3 public static ti void main(string[] i args) { 4 List<String> list = new LinkedList<>(); 5 list.add("america"); 6 list.add("canada"); 7 list.add("russia"); 8 list.add("france"); 9 // System.out.println("list: " + list); 10 ListIterator it = list.listiterator(); 11 while(it.hasnext()){ hasnext()){ 12 System.out.println(it.next()); 13 } 14 } 15 } output America Canada Russia France 16

17 The Vector Class, cont. List Vector +addelement(o: Object): void +capacity(): int +copyinto(anarray: Object[]): void +elementat(index: int): Object +elements(): Enumeration +ensurecapacity(): void +firstelement(): Object +insertelementat(o: Object, index: int): void +lastelement(): Object +removeallelements() : void +removeelement(o: Object) : boolean +removeelementat(index: int) : void +setelementat(o: Object, index: int) : void +setsize(newsize: int) : void +trimtosize() : void Appends the element to the end of this vector Returns the current capacity of this vector Copies the elements in this vector to the array Returns the object at the specified index Returns an emulation of this vector Increases the capacity of this vector Returns the first element in this vector Inserts o to this vector at the specified index Returns the last element in this vector Removes all the elements in this vector Removes the first matching element in this vector Removes the element at the specified index Sets a new element at the specified index Sets a new size in this vector Trims the capacity of this vector to its size 17

18 อ นเตอร เฟส Enumeration ภายในคลาส Vector ม เมธอดช อ มเมธอดชอ elements( ) ท ส งค า ทสงคา กล บเป น Enumeration เมธอดท ส าค ญ boolean hasmoreelement() Object nextelement() 18

19 Example:Vector, Enumeration 1 import java.util.*; 2 3 public class TestVecotorEnumeration t ti { 4 public static void main(string[] args) { 5 Vector<String> v = new Vector<>(); 6 v.add("america"); 7 v.add("canada"); 8 v.add("russia"); 9 v.add("france"); 10 Enumeration e = v.elements(); 11 while(e.hasmoreelements()){ hasmoreelements()){ 12 System.out.println(e.nextElement()+","); 13 } 14 } 15 } output America, Canada, Russia, France, 19

20 The Map Interface The Map interface maps keys to the elements. The keys are like indexes. In List, the indexes are integer. In Map, the keys can be any objects. The HashMap class is a concrete implementation of the Map interface. ม การเก บค าค ย ค ก บค าข อม ลของสมาช กเสมอ ค ย ห ามซ คยหามซาแตขอม ลซาได าแต ข อมลซ าได 20

21 The Map Interface UML Diagram Map +clear(): void +containskey(key: Object): boolean +containsvalue(value: Object): boolean +entryset(): Set +get(key: Object): Object +isempty(): boolean +keyset(): Set +put(key: Object, value: Object): Object +putall(m: Map): void +remove(key: Object): Object +size(): int +values(): Collection Removes es all mappings from this map Returns true if this map contains a mapping for the specified key. Returns true if this map maps one or more keys to the specified value. Returns a set consisting of the entries in this map Returns the value for the specified key in this map Returns true if this map contains no mappings Returns a set consisting of the keys in this map Puts a mapping in this map Adds all mappings from m to this map Removes the mapping for the specified key Returns the number of mappings in this map Returns a collection consisting of values in this map 21

22 Example: HashMap 1 import java.util.*; 2 3 public class TestMap { 4 public static void main(string[] args) { m.put("3","russia"); Map<String,String> m = new HashMap<>(); m.put("1","america"); m.put("2","canada"); 9 m.put("4","america"); 10 System.out.println("Befor duplicate key:"+ m); 11 m.put("2","thailand"); 12 System.out.println("After duplicate key:"+m); 13 System.out.println("first one: "+m.get("1")); 14 } 15 } Befor duplicate key:{3=russia, 2=Canada, 1=America, 4=America} After duplicate key:{3=russia, 2=Thailand, 1=America, 4=America} first one: America 22

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

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College 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

More information

An Interface with Generics

An Interface with Generics Generics CSC207 Software Design Generics An Interface with Generics Generics class foo introduces a class with a type parameter T. introduces a type parameter that is required to be

More information

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

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University C O P Y R I G H T S 2 0 1 5 E O M, H Y E O N S A N G A L L R I G H T S R E S E R V E D - Containers - About Containers

More information

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

Arrays organize each data element as sequential memory cells each accessed by an index. data data data data data data data data 1 JAVA PROGRAMMERS GUIDE LESSON 1 File: JGuiGuideL1.doc Date Started: July 10, 2000 Last Update: Jan 2, 2002 Status: proof DICTIONARIES, MAPS AND COLLECTIONS We have classes for Sets, Lists and Maps and

More information

CONTAİNERS COLLECTİONS

CONTAİNERS COLLECTİONS CONTAİNERS Some programs create too many objects and deal with them. In such a program, it is not feasible to declare a separate variable to hold reference to each of these objects. The proper way of keeping

More information

Sets and Maps. CSE260, Computer Science B: Honors Stony Brook University

Sets and Maps. CSE260, Computer Science B: Honors Stony Brook University Sets and Maps CSE260, Computer Science B: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse260 1 Objectives To store unordered, nonduplicate elements using a set. To explore how and when

More information

Lecture 6 Collections

Lecture 6 Collections Lecture 6 Collections Concept A collection is a data structure actually, an object to hold other objects, which let you store and organize objects in useful ways for efficient access Check out the java.util

More information

Frameworks. CS151 Chris Pollett Oct. 26, 2005.

Frameworks. CS151 Chris Pollett Oct. 26, 2005. Frameworks CS151 Chris Pollett Oct. 26, 2005. Outline Collections Framework GUI Frameworks - AWT and Swing The Java Collection Framework Last day, we began discussing the Java collection Framework. We

More information

Java Collections Framework: Interfaces

Java Collections Framework: Interfaces Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection Interface The List Interface The Iterator Interface The ListIterator

More information

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

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering Readings and References Java Collections "Collections", Java tutorial http://java.sun.com/docs/books/tutorial/collections/index.html CSE 403, Spring 2004 Software Engineering http://www.cs.washington.edu/education/courses/403/04sp/

More information

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

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1 Topic #9: Collections CSE 413, Autumn 2004 Programming Languages http://www.cs.washington.edu/education/courses/413/04au/ If S is a subtype of T, what is S permitted to do with the methods of T? Typing

More information

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

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering Readings and References Java Collections References» "Collections", Java tutorial» http://java.sun.com/docs/books/tutorial/collections/index.html CSE 403, Winter 2003 Software Engineering http://www.cs.washington.edu/education/courses/403/03wi/

More information

EECS 2011 M: Fundamentals of Data Structures

EECS 2011 M: Fundamentals of Data Structures EECS 2011 M: Fundamentals of Data Structures Suprakash Datta Office: LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle S. Datta (York Univ.) EECS 2011 W18 1 / 19 Iterators and

More information

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

Overview of Java ArrayList, HashTable, HashMap, Hashet,LinkedList Overview of Java ArrayList, HashTable, HashMap, Hashet,LinkedList This article discusses the main classes of Java Collection API. The following figure demonstrates the Java Collection framework. Figure

More information

Generics Collection Framework

Generics Collection Framework Generics Collection Framework Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad Website: www.sisoft.in Email:info@sisoft.in Phone: +91-9999-283-283 Generics

More information

The Java Collections Framework. Chapters 7.5

The Java Collections Framework. Chapters 7.5 The Java s Framework Chapters 7.5 Outline Introduction to the Java s Framework Iterators Interfaces, Classes and Classes of the Java s Framework Outline Introduction to the Java s Framework Iterators Interfaces,

More information

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

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock) Lecture 15 Summary Collections Framework Iterable, Collections List, Set Map Collections class Comparable and Comparator 1 By the end of this lecture, you will be able to use different types of Collections

More information

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

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map Lecture 15 Summary Collections Framework Iterable, Collections List, Set Map Collections class Comparable and Comparator 1 By the end of this lecture, you will be able to use different types of Collections

More information

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

Abstract data types (again) Announcements. Example ADT an integer bag (next) The Java Collections Framework Announcements Abstract data types (again) PS 5 ready Tutoring schedule updated with more hours Today s topic: The Java Collections Framework Reading: Section 7.5 An ADT is a model of a collection of data

More information

Questions and Answers. Q.3) asses is not part of Java's collection framework?explanation:

Questions and Answers.   Q.3) asses is not part of Java's collection framework?explanation: Q.1) $ javac vector.java $ java vector 4 A. [3, 2, 6] B. [3, 2, 8] C. [3, 2, 6, 8] D. [3, 2, 8, 6] ANSWER : [3, 2, 8, 6] $ javac vector.java $ java vector [3, 2, 8, 6] Q.2) Which interface does java.util.hashtable

More information

Collections Framework: Part 2

Collections Framework: Part 2 Collections Framework: Part 2 Computer Science and Engineering College of Engineering The Ohio State University Lecture 18 Collection Implementations Java SDK provides several implementations of Collection

More information

IS311. Data Structures and Java Collections

IS311. Data Structures and Java Collections IS311 Data Structures and Java Collections 1 Algorithms and Data Structures Algorithm Sequence of steps used to solve a problem Operates on collection of data Each element of collection -> data structure

More information

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

Type Parameters: E - the type of elements returned by this iterator Methods Modifier and Type Method and Description java.lang Interface Iterable Type Parameters: T - the type of elements returned by the iterator Iterator iterator() Returns an iterator over a set of elements of type T. java.util Interface Iterator

More information

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

Framework in Java 5. DAAD project Joint Course on OOP using Java Topic XXX Collections Framework in Java 5 DAAD project Joint Course on OOP using Java Humboldt University Berlin, University of Novi Sad, Polytehnica University of Timisoara, University of Plovdiv, University

More information

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

CISC 3115 TY3. C28a: Set. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/29/2018 CUNY Brooklyn College CISC 3115 TY3 C28a: Set Hui Chen Department of Computer & Information Science CUNY Brooklyn College 11/29/2018 CUNY Brooklyn College 1 Outline Discussed Concept of data structure Use data structures List

More information

Lecture 4. The Java Collections Framework

Lecture 4. The Java Collections Framework Lecture 4. The Java s Framework - 1 - Outline Introduction to the Java s Framework Iterators Interfaces, Classes and Classes of the Java s Framework - 2 - Learning Outcomes From this lecture you should

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

Sets and Maps. Part of the Collections Framework

Sets and Maps. Part of the Collections Framework 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 int size( ); boolean isempty( ); boolean contains(object

More information

Class 32: The Java Collections Framework

Class 32: The Java Collections Framework Introduction to Computation and Problem Solving Class 32: The Java Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward Goals To introduce you to the data structure classes that come

More information

Chapter 5 Java Collection. Wang Yang

Chapter 5 Java Collection. Wang Yang Chapter 5 Java Collection Wang Yang wyang@njnet.edu.cn Outline Last Chapter Review Array & Arrays Tourism Collection & Collections Map Performance benchmark Last Chapter Review Exception Exception is an

More information

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE PREVIOUS COURSE CONTENT Inheritance Abstract classes Interfaces instanceof operator Nested classes Enumerations COUSE CONTENT Collections List Map Set Aggregate

More information

Java Collections Framework. 24 April 2013 OSU CSE 1

Java Collections Framework. 24 April 2013 OSU CSE 1 Java Collections Framework 24 April 2013 OSU CSE 1 Overview The Java Collections Framework (JCF) is a group of interfaces and classes similar to the OSU CSE components The similarities will become clearly

More information

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

Software 1 with Java. Java Collections Framework. Collection Interfaces. Online Resources. The Collection Interface Java Collections Framework Collection: a group of elements Based Design: Software 1 with Java Java Collections Framework s s Algorithms Recitation No. 6 (Collections) 2 Collection s Online Resources Collection

More information

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

Collections (Collection Framework) Sang Shin Java Technology Architect Sun Microsystems, Inc. Collections (Collection Framework) Sang Shin Java Technology Architect Sun Microsystems, Inc. sang.shin@sun.com www.javapassion.com 2 Disclaimer & Acknowledgments Even though Sang Shin is a full-time employee

More information

CS Ananda Gunawardena

CS Ananda Gunawardena CS 15-121 Ananda Gunawardena A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve and manipulate data,

More information

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

Software 1 with Java. Recitation No. 6 (Collections) Software 1 with Java Recitation No. 6 (Collections) Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework Interfaces Implementations Algorithms 2

More information

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

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss The Collections API Mark Allen Weiss Lecture Objectives To learn how to use the Collections package in Java 1.2. To illustrate features of Java that help (and hurt) the design of the Collections API. Tuesday,

More information

Collections and Maps

Collections and Maps Software and Programming I Collections and Maps Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline Array Lists Enhanced for Loop ArrayList and LinkedList Collection Interface Sets and

More information

What is the Java Collections Framework?

What is the Java Collections Framework? 1 of 13 What is the Java Collections Framework? To begin with, what is a collection?. I have a collection of comic books. In that collection, I have Tarzan comics, Phantom comics, Superman comics and several

More information

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

Pieter van den Hombergh Richard van den Ham. February 8, 2018 Pieter van den Hombergh Richard van den Ham Fontys Hogeschool voor Techniek en Logistiek February 8, 2018 /FHTenL February 8, 2018 1/16 Collection Zoo The basic collections, well known in programming s

More information

Computer Science II (Spring )

Computer Science II (Spring ) Computer Science II 4003-232-01 (Spring 2007-2008) Week 5: Generics, Java Collection Framework Richard Zanibbi Rochester Institute of Technology Generic Types in Java (Ch. 21 in Liang) What are Generic

More information

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

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures Collections CSE 143 Java Collections Most programs need to store and access collections of data Collections are worth studying because... They are widely useful in programming They provide examples of

More information

Why Use Generics? Generic types Generic methods Bounded type parameters Generics, Inheritance, and Subtypes Wildcard types

Why Use Generics? Generic types Generic methods Bounded type parameters Generics, Inheritance, and Subtypes Wildcard types Why Use Generics? Generic types Generic methods Bounded type parameters Generics, Inheritance, and Subtypes Wildcard types generics enable types (classes and interfaces) to be parameters when defining

More information

Implementing Lists, Stacks, Queues, and Priority Queues

Implementing Lists, Stacks, Queues, and Priority Queues Implementing Lists, Stacks, Queues, and Priority Queues CSE260, Computer Science B: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse260 1 Objectives To design common features of lists in

More information

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.

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. Lecture 15 Summary Collections Framework Iterable, Collections, Set Map Collections class Comparable and Comparator By the end of this lecture, you will be able to use different types of Collections and

More information

Some examples and/or figures were borrowed (with permission) from slides prepared by Prof. H. Roumani. The Collection Framework

Some examples and/or figures were borrowed (with permission) from slides prepared by Prof. H. Roumani. The Collection Framework Some examples and/or figures were borrowed (with permission) from slides prepared by Prof. H. Roumani The Collection Framework Collection: an aggregate that can hold a varying number of elements Interface:

More information

DM550 Introduction to Programming part 2. Jan Baumbach.

DM550 Introduction to Programming part 2. Jan Baumbach. DM550 Introduction to Programming part 2 Jan Baumbach jan.baumbach@imada.sdu.dk http://www.baumbachlab.net MULTIVARIATE TREES 2 Multivariate Trees general class of trees nodes can have any number of children

More information

9/16/2010 CS Ananda Gunawardena

9/16/2010 CS Ananda Gunawardena CS 15-121 Ananda Gunawardena A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve and manipulate data,

More information

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

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University C O P Y R I G H T S 2 0 1 5 E O M, H Y E O N S A N G A L L R I G H T S R E S E R V E D - String - Overloading -

More information

DM550 Introduction to Programming part 2. Jan Baumbach.

DM550 Introduction to Programming part 2. Jan Baumbach. DM550 Introduction to Programming part 2 Jan Baumbach jan.baumbach@imada.sdu.dk http://www.baumbachlab.net Sorting Tree Balancing A sorted tree is balanced iff for each node holds: Math.abs(size(node.left)

More information

תוכנה 1 מבני נתונים גנריים

תוכנה 1 מבני נתונים גנריים תוכנה 1 מבני נתונים גנריים תרגול 8 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework Interfaces Implementations Algorithms 2 Online Resources

More information

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides adapted from Marty Stepp and Hélène Martin http://www.cs.washington.edu/143/ Related classes Consider classes for

More information

Collections Questions

Collections Questions Collections Questions https://www.journaldev.com/1330/java-collections-interview-questions-and-answers https://www.baeldung.com/java-collections-interview-questions https://www.javatpoint.com/java-collections-interview-questions

More information

Java Collections Framework reloaded

Java Collections Framework reloaded Java Collections Framework reloaded October 1, 2004 Java Collections - 2004-10-01 p. 1/23 Outline Interfaces Implementations Ordering Java 1.5 Java Collections - 2004-10-01 p. 2/23 Components Interfaces:

More information

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

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. June 8, 2017 Pieter van den Hombergh Thijs Dorssers Stefan Sobek Fontys Hogeschool voor Techniek en Logistiek June 8, 2017 /FHTenL June 8, 2017 1/19 Collection Zoo The basic collections, well known in programming s

More information

CS2110: Software Development Methods. Maps and Sets in Java

CS2110: Software Development Methods. Maps and Sets in Java CS2110: Software Development Methods Maps and Sets in Java These slides are to help with the lab, Finding Your Way with Maps Today s lab uses Maps (and Sets but just a little). Readings from textbook:

More information

CS2110: Software Development Methods. Maps and Sets in Java

CS2110: Software Development Methods. Maps and Sets in Java CS2110: Software Development Methods Maps and Sets in Java These slides are to help with the lab, Finding Your Way with Maps This lab uses Maps, and Sets too (but just a little). Readings from textbook:

More information

Algorithms. Produced by. Eamonn de Leastar

Algorithms. Produced by. Eamonn de Leastar Algorithms Produced by Eamonn de Leastar (edeleastar@wit.ie) Collections ± Collections Architecture ± Definition ± Architecture ± Interfaces ± Collection ± List ± Set ± Map ± Iterator ± Implementations

More information

Chapter 10 Collections

Chapter 10 Collections Chapter 10 Collections I. Scott MacKenzie 1 Outline 2 1 What is a Collection? A collection is an aggregation with a variable number of components, or elements Examples Portfolio - a collection of Investment

More information

Object-Oriented Programming with Java

Object-Oriented Programming with Java Object-Oriented Programming with Java Recitation No. 2 Oranit Dror The String Class Represents a character string (e.g. "Hi") Explicit constructor: String quote = "Hello World"; string literal All string

More information

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

Java Data Structures Collections Framework BY ASIF AHMED CSI-211 (OBJECT ORIENTED PROGRAMMING) Java Data Structures Collections Framework BY ASIF AHMED CSI-211 (OBJECT ORIENTED PROGRAMMING) What is a Data Structure? Introduction A data structure is a particular way of organizing data using one or

More information

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

27/04/2012. Objectives. Collection. Collections Framework. Collection Interface. Collection algorithm. Legacy collection Objectives Collection Collections Framework Concrete collections Collection algorithm By Võ Văn Hải Faculty of Information Technologies Summer 2012 Legacy collection 1 2 2/27 Collections Framework "Collection"

More information

C Programming

C Programming 204216 -- C Programming Chapter 5 Repetition Adapted/Assembled for 204216 by Areerat Trongratsameethong Objectives Basic Loop Structures The while Statement Computing Sums and Averages Using a while Loop

More information

Principles of Software Construction: Objects, Design and Concurrency. More design patterns and Java Collections. toad

Principles of Software Construction: Objects, Design and Concurrency. More design patterns and Java Collections. toad Principles of Software Construction: Objects, Design and Concurrency 15-214 toad More design patterns and Java Collections Spring 2013 Christian Kästner Charlie Garrod School of Computer Science 2012-13

More information

Get started with the Java Collections Framework By Dan Becker

Get started with the Java Collections Framework By Dan Becker Get started with the Java Collections Framework By Dan Becker JavaWorld Nov 1, 1998 COMMENTS JDK 1.2 introduces a new framework for collections of objects, called the Java Collections Framework. "Oh no,"

More information

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

Recap. List Types. List Functionality. ListIterator. Adapter Design Pattern. Department of Computer Science 1 COMP209 Object Oriented Programming Container Classes 3 Mark Hall List Functionality Types List Iterator Adapter design pattern Adapting a LinkedList to a Stack/Queue Map Functionality Hashing Performance

More information

Collections. James Brucker

Collections. James Brucker Collections James Brucker Collection A Collection is a group of objects. Set an unordered collection no duplicates List ordered collection duplicates are allowed can add or remove elements anywhere in

More information

JAVA. java.lang.stringbuffer java.lang.stringbuilder

JAVA. java.lang.stringbuffer java.lang.stringbuilder JAVA java.lang.stringbuffer java.lang.stringbuilder 1 Overview mutable string instances of String are immutable do not extend String String, StringBuffer, StringBuilder are final StringBuffer safe for

More information

Vector (Java 2 Platform SE 5.0) Overview Package Class Use Tree Deprecated Index Help

Vector (Java 2 Platform SE 5.0) Overview Package Class Use Tree Deprecated Index Help Overview Package Class Use Tree Deprecated Index Help PREV CLASS NEXT CLASS FRAMES NO FRAMES All Classes SUMMARY: NESTED FIELD CONSTR METHOD DETAIL: FIELD CONSTR METHOD Página 1 de 30 Java TM 2 Platform

More information

Java Collections. Wrapper classes. Wrapper classes

Java Collections. Wrapper classes. Wrapper classes Java Collections Engi- 5895 Hafez Seliem Wrapper classes Provide a mechanism to wrap primitive values in an object so that the primitives can be included in activities reserved for objects, like as being

More information

Java Collections. Engi Hafez Seliem

Java Collections. Engi Hafez Seliem Java Collections Engi- 5895 Hafez Seliem Wrapper classes Provide a mechanism to wrap primitive values in an object so that the primitives can be included in activities reserved for objects, like as being

More information

Collections IS311. The Collections Framework. Type Trees for Collections. Java Collections Framework (ต อ)

Collections IS311. The Collections Framework. Type Trees for Collections. Java Collections Framework (ต อ) IS311 Java Collections Framework (ต อ) Collections Collections are holders that let you store and organize objects in useful ways for efficient access. In the package java.util, there are interfaces and

More information

IS311. Java Collections Framework (ต อ)

IS311. Java Collections Framework (ต อ) IS311 Java Collections Framework (ต อ) 1 Collections Collections are holders that let you store and organize objects in useful ways for efficient access. In the package java.util, there are interfaces

More information

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

[Ref: Core Java Chp 13, Intro to Java Programming [Liang] Chp 22, Absolute Java Chp 16, docs.oracle.com/javase/tutorial/collections/toc. Contents Topic 08 - Collections I. Introduction - Java Collection Hierarchy II. Choosing/using collections III. Collection and Iterator IV. Methods of Collection V. Concrete classes VI. Implementation

More information

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

Important Dates. Game State and Tree. Today s topics. Game Tree and Mini-Max. Games and Mini-Max 3/20/14 MINI-MAX USING TREES AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 Spring 2014 2 Important Dates. April 10 --- A4 due (Connect 4, minimax, trees) April 15 --- A5 due (Exercises on different topics,

More information

Since it is an interface, we need a concrete class during its declaration. There are many ways to initialize a Queue object, most common being-

Since it is an interface, we need a concrete class during its declaration. There are many ways to initialize a Queue object, most common being- Laboratory 2 Data Structures-II Implementation of Daa structures using Collection frame work Obecjective Data Structures. A data structure is a particular way of storing and organizing data in a computer

More information

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

CSE 373. Java Collection Framework. reading: Weiss Ch. 3, 4.8. slides created by Marty Stepp CSE 373 Java Collection Framework reading: Weiss Ch. 3, 4.8 slides created by Marty Stepp http://www.cs.washington.edu/373/ 1 Arrays array: An object that stores many values of the same type. element:

More information

Notes on access restrictions

Notes on access restrictions Notes on access restrictions A source code file MyClass.java is a compilation unit and can contain at most one public class. Furthermore, if there is a public class in that file, it must be called MyClass.

More information

SUMMARY INTRODUCTION COLLECTIONS FRAMEWORK. Introduction Collections and iterators Linked list Array list Hash set Tree set Maps Collections framework

SUMMARY INTRODUCTION COLLECTIONS FRAMEWORK. Introduction Collections and iterators Linked list Array list Hash set Tree set Maps Collections framework SUMMARY COLLECTIONS FRAMEWORK PROGRAMMAZIONE CONCORRENTE E DISTR. Università degli Studi di Padova Dipartimento di Matematica Corso di Laurea in Informatica, A.A. 2015 2016 Introduction Collections and

More information

CS61B Lecture #17. Last modified: Mon Oct 1 13:40: CS61B: Lecture #17 1

CS61B Lecture #17. Last modified: Mon Oct 1 13:40: CS61B: Lecture #17 1 CS61B Lecture #17 Last modified: Mon Oct 1 13:40:29 2018 CS61B: Lecture #17 1 Topics Overview of standard Java Collections classes Iterators, ListIterators Containers and maps in the abstract Amortized

More information

Introduction to Collections

Introduction to Collections Module 3 COLLECTIONS Introduction to Collections > A collection sometimes called a container is simply an object that groups multiple elements into a single unit. > Collections are used to store, retrieve,

More information

Adam Blank Lecture 5 Winter 2019 CS 2. Introduction to Programming Methods

Adam Blank Lecture 5 Winter 2019 CS 2. Introduction to Programming Methods Adam Blank Lecture 5 Winter 2019 CS 2 Introduction to Programming Methods CS 2: Introduction to Programming Methods Java Collections Abstract Data Types (ADT) 1 Abstract Data Type An abstract data type

More information

Tha Java Programming Language

Tha Java Programming Language Tha Java Programming Language Kozsik Tamás (2000-2001) kto@elte.hu http://www.elte.hu/~k to/ III. Arrays, collections and other baseclasses Some of the baseclasses Object String StringBuffer Integer, Double,...

More information

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

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces Taking Stock IE170: Algorithms in Systems Engineering: Lecture 7 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University January 29, 2007 Last Time Practice Some Sorting Algs

More information

Top level classes under java.util

Top level classes under java.util collections The java.util package also contains one of Java s most powerful subsystems: the Collections Framework. The Collections Framework is a sophisticated hierarchy of interfaces and classes that

More information

Java Collections Framework

Java Collections Framework Java Collections Framework Introduction In this article from my free Java 8 course, you will be given a high-level introduction of the Java Collections Framework (JCF). The term Collection has several

More information

JAVA. java.lang.stringbuffer java.lang.stringbuilder

JAVA. java.lang.stringbuffer java.lang.stringbuilder JAVA java.lang.stringbuffer java.lang.stringbuilder 1 Overview mutable string instances of String are immutable do not extend String String, StringBuffer, StringBuilder are final StringBuffer safe for

More information

First Name Last Name ID#

First Name Last Name ID# CIS 265 Sect 01 - V. Matos Exam-2 Spring-2015 First Name Last Name ID# (15 pts) MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) In the following

More information

Programmieren II. Collections. Alexander Fraser. May 28, (Based on material from T. Bögel)

Programmieren II. Collections. Alexander Fraser. May 28, (Based on material from T. Bögel) Programmieren II Collections Alexander Fraser fraser@cl.uni-heidelberg.de (Based on material from T. Bögel) May 28, 2014 1 / 46 Outline 1 Recap Paths and Files Exceptions 2 Collections Collection Interfaces

More information

CS61B Lecture #17. Administrative: Need alternative test time? Make sure you send me mail.

CS61B Lecture #17. Administrative: Need alternative test time? Make sure you send me mail. Administrative: CS61B Lecture #17 Need alternative test time? Make sure you send me mail. Last modified: Fri Oct 18 14:48:51 2013 CS61B: Lecture #17 1 Topics Overview of standard Java Collections classes.

More information

CS/CE 2336 Computer Science II

CS/CE 2336 Computer Science II CS/CE 2336 Computer Science II UT D Session 11 OO Data Structures Lists, Stacks, Queues Adapted from D. Liang s Introduction to Java Programming, 8 th Ed. 2 What is a Data Structure? A collection of data

More information

Collections. The Java Collections Framework. The Interfaces

Collections. The Java Collections Framework. The Interfaces Collections A collection (sometimes called a container) is an object that groups multiple elements into a single unit. Collections are used to store, retrieve and manipulate data, and to transmit data

More information

Topics. CS61B Lecture #17. Collection Structures in java.util. Data Types in the Abstract

Topics. CS61B Lecture #17. Collection Structures in java.util. Data Types in the Abstract CS61B Lecture #17 Topics Administrative: Need alternative test time? Make sure you send me mail. Overview of standard Java Collections classes. Iterators, ListIterators Containers and maps in the abstract

More information

CIS 265 Exam 2 First Name Last Name

CIS 265 Exam 2 First Name Last Name CIS 265 Exam 2 First Name Last Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Which of the data types below does not allow duplicates? 1)

More information

Advanced Programming Generics Collections

Advanced Programming Generics Collections Advanced Programming Generics Collections The Context Create a data structure that stores elements: a stack, a linked list, a vector a graph, a tree, etc. What data type to use for representing the elements

More information

CSE 143 Lecture 4. Preconditions

CSE 143 Lecture 4. Preconditions CSE 143 Lecture 4 Exceptions and ArrayList slides created by Marty Stepp http://www.cs.washington.edu/143/ Preconditions precondition: Something your method assumes is true at the start of its execution.

More information

List... NOTE We use List as illustrative example of collections Study the others from textbook!

List... NOTE We use List as illustrative example of collections Study the others from textbook! List... NOTE We use List as illustrative example of collections Study the others from textbook! 25 What is a List? A List is an ordered Collection (sometimes called a sequence) Lists may contain duplicate

More information

Constraining Type Variables

Constraining Type Variables Constraining Type Variables Type variables can be constrained with bounds. It is often necessary to specify what types can be used in a generic class or method. Consider a generic min method that finds

More information

23 Sorting a text file using an ArrayList 24 The SortList class? 26 The SortList class? 27 Collections API: Collections class 30 The SortList class? 3

23 Sorting a text file using an ArrayList 24 The SortList class? 26 The SortList class? 27 Collections API: Collections class 30 The SortList class? 3 List of Slides 1 Title 2 Chapter 21: Collections 3 Chapter aims 4 Section 2: Example:Reversing a text file 5 Aim 6 Reversing a text file 7 Collections API 8 Collections API: Lists 9 Collections API: Lists:

More information

Basicsof programming3. Java collections

Basicsof programming3. Java collections Basicsof programming3 Java collections Java Generics Basics of programming 3 BME IIT, Goldschmidt Balázs 2 Generics Objective: code reuse with generic types Csolution void* malloc(size_t s) casting is

More information