Java Collections Framework The client view. The Big Picture.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Chapter 24 Lists, Stacks, and Queues
Concrete collections in Java library
JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CS Data Structures II Review COSC 2006 April 14, 2017
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
1 Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C , 9.3.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
COMP 110 Introduction to Programming Mr. Joshua Stough.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques
Java's Collection Framework
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
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.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
1 Sets and Maps Starring: keySet Co-Starring: Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
Chapter 18 Java Collections Framework
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Data structures and algorithms in the collection framework 1.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Linked Data Structures
Sets and Maps Chapter 9.
Building Java Programs
Queues Chapter 8 (continued)
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 17 Object-Oriented Data Structures
Building Java Programs
structures and their relationships." - Linus Torvalds
Java Collections Framework
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Sets and Maps Chapter 9.
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Queues.
Presentation transcript:

Java Collections Framework The client view

The Big Picture

Abstract Data Type Operations supported o CRUD o Membership management o Misc. Defined as interfaces, such as o Collection o List o Queue o Set o Iterable ADT data CRUD

List: An ADT We Already Know Real world exampleWays to manipulate Shopping list o Elements listed in a certain order (position can be labelled with an index) Add, remove, or modify anywhere in the list Order in the list matters Know what’s on the list or not Know count of entries on the list List specs in Java API

List: The Abstract View A Sequence of ElementsTwo popular ways With no rules how they can be accessed To implement a list o Using an array: for fast access o Using a “link” structure: for easy insertion and deletion (in the middle or anywhere else)

Queue: Another Linear Structure A Real World ExampleWays To Manipulate A service queue o A line of customers to be serviced General rule: o The head and tail are known o First come first serviced or FIFO o Size of the queue may be limited Add: only to the back (or tail)  enqueue Remove: only from the front (or head)  dequeue

Queue: The Abstract View No arbitrary accessJava API is allowed Operations as specified in java.util.Queue interface head tail dequeue enqueue Throws exception Returns special value Insertadd(e)offer(e) Removeremove()poll() Examineelement()peek() NoSuchElementException if queue is empty, or IllegalStateException if size limit reached false null

Stack A real world exampleWays To Manipulate A pile of rings that can only be moved (on or off) from the top LIFO: It’s opposite to the order enforced by queue top pop push Return TypeMethod booleanemptyempty() Epeekpeek() Epoppop() Epushpush(E item)E intsearchsearch(Object o)Object Provided as a class since Java 1.0a class since Java 1.0 Index is 1 -based

Set: Unordered But No-duplicates Mathematically, a set is defined as “A collection that contains no duplicate elements”, and ordering is not of concern. Two implementing classes often used in Java API:often used in Java API -TreeSet, in which elements are ordered using their natural orderingnatural ordering -HashSet, for which no iteration ordering guaranteed

Map: Collection of K-V Pairs In the Java API, Map is specified as an interface: o An object that maps keys to values. o A map cannot contain duplicate keys; o each key can map to at most one value. Map Key set Value collection Map.Entry The CRUD Operations C  put(K key, V value) : insert a new entry when the key is not in the Map yet R  get(Object key) U  put(K key, V value) : replace the value associate with an existing key to a new value D  remove(Object key)

Iterable: Super-Interface of All All Collection objects need to provide a way to guide its client to iterate (or visit each element just once) through its collection. o Made available by implementing the iterator() method as specified in the Collection interface since Java 1.2. Iterable, a super interface to Collection is added in Java 1.5: o This interface requires only one method: iterator(). o Implementing this interface allows an object to be the target of the "for- each" statement. Iterable c = …; for (T t : c) System.out.println(t); Iterable c = …; Iterator itr = c.iterator(); while(itr.hasNext()) System.out.println(itr.next()); Iterator used implicitly.

Iterating thru a Map Map is not a subtype of Iterable. Its contents need to be iterated based on o Its key set, or o Its value collection > Iterable keySet() returns all keys in a Set values() returns all values in a Collection

Map as a Frequency Table Map freqTbl = TreeMap(); String text = …; Character ch; for (int i=0; i<text.length(); i++) { ch = text.charAt(i); if (freqTbl.get(ch) == null) freqTbl.put(ch, 1); else freqTbl.put(ch, freqTbl.get(ch) + 1); } Set chs = freqTbl.keySet(); for (Character c : chs) System.out.println(c + " --> " + freqTbl.get(c)); }

A Map Of Lists In the Jeopardy! game, a map can be used to relate category names to the lists of questions Key Value Map qByCats = new Map (); Set cats = qByCats.keySet(); for (K key : cats) System.out.println(key + " --> " + qByCats.get(key)); A --> [A0, A1, A2, A3, A4] B --> [B0, B1, B2, B3, B4] C --> [C0, C1, C2, C3, C4] D --> [D0, D1, D2, D3, D4] E --> [E0, E1, E2, E3, E4] F --> [F0, F1, F2, F3, F4]

Key Value A Map Of Lists Display the map contents in the desired way ABCDEF A0B0C0D0E0F0 A1B1C1D1E1F1 A2B2C2D2E2F2 A3B3C3D3E3F3 A4B4C4D4E4F4 Map qByCats = new Map (); Set cats = qByCats.keySet(); //display category names in a row for (String k : keys) { System.out.print(k + '\t'); } System.out.println(); //list as a column beneath category for (int i=0; i<5; i++) { for (String k : keys) { System.out.print(m.get(k).get(i) + '\t'); } System.out.println(); }

Sorting w/ a New Order Comparator c = new Comparator () public int compare(String priceS1, String priceS2) { return Integer.parseInt(priceS1.substring(1)) - Integer.parseInt(priceS2.substring(1)); } }; for (String k : keys) { System.out.print(k + ‘\t'); Collections.sort(m.get(k), c); } System.out.print( formatToLength(colWidth, k) + ' '); Sorting w/ a Comparator Format to a fixed length import java.util.Comparator;