Big Java Chapter 16.

Slides:



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

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
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.
Chapter 10: Data Structures II
Processing Data in Collections Chapter Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects.
Copyright © 2013 by John Wiley & Sons. All rights reserved. THE JAVA COLLECTIONS FRAMEWORK CHAPTER Slides by Donald W. Smith TechNeTrain.com 15.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
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.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 16.  Data structures that take control of organizing elements  Elements not in fixed positions  Advantage – better performance Adding Removing.
Chapter 19 Java Data Structures
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
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.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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.
DataStructures1 Barb Ericson Georgia Tech July 2008.
Data structures and algorithms in the collection framework 1 Part 2.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Chapter 18 Java Collections Framework
Data structures and algorithms in the collection framework 1.
CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
Georgia Institute of Technology Workshop for CS-AP Teachers Data Structures Barb Ericson June 2006.
Hashing Hashing is another method for sorting and searching data.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 6 Data Structures.
Hashing as a Dictionary Implementation Chapter 19.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Hash Tables and Hash Maps. DCS – SWC 2 Hash Tables A Set and a Map are both abstract data types – we need a concrete implemen- tation in order to use.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
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.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
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.
Hashing By Emily Nelson. The Official Definition Using a hash function to turn some kind of data in relatively small integers or Strings The “hash code”
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Sets and Maps Chapter 9.
Slides by Donald W. Smith
Sections 10.5 – 10.6 Hashing.
Slides by Donald W. Smith
Chapter 19 Java Data Structures
Chapter 10: Non-linear Data Structures
Road Map CS Concepts Data Structures Java Language Java Collections
Sets, Maps and Hash Tables
Sets and Maps Chapter 9.
Presentation transcript:

Big Java Chapter 16

Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary trees Heap Priority queue

Sets Unordered collection Fundamental operations: Reject duplicates Add an element Remove an element Containment test (is object in set?) List elements (in arbitrary order) Reject duplicates

<<interface>> Set Implementation <<interface>> Set HashSet TreeSet

Example instantiate with specific implementation. Advantage: could easily change implementation, rest of program stays the same. use methods from interface could also do: if (names.contains(“Jane”)) { … }

Example (continued) can use “for each” loop with Sets Nodes are not necessarily visited in order inserted!

Reading Assignment Read Quality Tip 16.1 pages 704-705. How would you feel if you had designed the List interface and ArrayList/LinkedList classes?

Maps A map data type keeps associations between keys and values Cannot contain duplicate keys Operations include: put get containsKey/containsValue keySet/values – return all keys/values size Java has two implementations: HashMap and TreeMap.

Example

Hash Table Hash table – can be used to implement sets and maps Hash function – computes an integer value (hash code) from an object, goal is for different objects to yield different hash codes int h = obj.hashCode(); hashCode is inherited from Object Collision – when two or more distinct objects have the same hash code

HashSet Author provides HashSet, an extension of AbstractSet provides a hash table with buckets (linked lists) to hold collisions possible hash code for string: final int HASH_MULTIPLER = 31; //prime int h=0; for (int i=0; i<s.length(); i++) h = HASH_MULTIPLIER * h + s.charAt(i); Can use integer fields directly as hash codes If hashCode not overridden, Object class computes based on memory location of object. Problem if define equals but not hashCode.

Binary Search Tree Review on your own, pages 720-734

TreeSet or HashSet? With a good hash function, hashing is usually faster Balanced trees (remember those?) can guarantee reasonable performance, HashSet depends entirely on performance of hash function To use TreeSet, objects being stored must implement Comparable interface For TreeMap, same requirement for keys Can supply a Comparator object to TreeSet/TreeMap constructor (takes two objects and returns comparison) TreeSet is preferable if want to print list of items in order

Reading Assignment Random Fact 16.2, Software Piracy, pages 738-739

Priority Queue Collects elements which have a priority Elements inserted in any order, but retrieved according to priority Java includes PriorityQueue() class Abstract data class Often use heap to implement Heaps covered in 406 (I think) Heapsort is O(n log(n))

Chapter 16 Quick Exercise Use the HashTable code from the textbook Can you store objects with the same hash code but different data? Try modifying CoinHashCodePrinter to insert two quarters but initialize them differently (e.g., “a quarter” and “quarter” rather than both “quarter”) Review the TreeSet and TreeSetTester code. Notice the use of CoinComparator. Modify to use with some other class, such as BankAccount