CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

Slides:



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

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.
Processing Data in Collections Chapter Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects.
Hashing as a Dictionary Implementation
Copyright © 2013 by John Wiley & Sons. All rights reserved. THE JAVA COLLECTIONS FRAMEWORK CHAPTER Slides by Donald W. Smith TechNeTrain.com 15.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
© 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.
Chapter 16.  Data structures that take control of organizing elements  Elements not in fixed positions  Advantage – better performance Adding Removing.
CSE 143 Lecture 15 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Chapter 19 Java Data Structures
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
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:
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
1 Concrete collections II. 2 HashSet hash codes are used to organize elements in the collections, calculated from the state of an object –hash codes are.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
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.
Hashing Hashing is another method for sorting and searching data.
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.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
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.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Priority Queues. Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue.
Nov 22, 2010IAT 2651 Java Collections. Nov 22, 2010IAT 2652 Data Structures  With a collection of data, we often want to do many things –Organize –Iterate.
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.
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
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.
Collections Dwight Deugo Nesa Matic
CSE 143 Lecture 11: Sets and Maps reading:
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Slides by Donald W. Smith
Slides by Donald W. Smith
Chapter 19 Java Data Structures
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
Ch 16: Data Structures - Set and Map
Road Map CS Concepts Data Structures Java Language Java Collections
Part of the Collections Framework
Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection.
CS2110: Software Development Methods
CSE 1020: The Collection Framework
slides created by Marty Stepp
Presentation transcript:

CSS446 Spring 2014 Nan Wang

 Java Collection Framework ◦ Set ◦ Map 2

 Set is an unordered collection of unique elements.  Because a set does not track the order of the elements, so the operations of finding, adding and removing are more efficient. 3

 The HashSet and TreeSet classes both implement the Set interface.  A set does not admit duplicates. If you add an element to a set that is already present, the insertion is ignored.  HashSet --- Hash Table  TreeSet --- Binary Search Tree  Set implementations arrange the elements so that they can locate them quickly. 4

 Set elements are grouped into smaller collections of elements that share the same characteristic.  You can imagine a hash set of books as having a group for each color, so that books of the same color are in the same group. To find whether a book is already present, you just need to check it against the books in the same color group.  Integer values (called hash codes) that can be computed from the elements 5

 Hash Table uses hash code which is computed from the elements to find, add and remove elements efficiently.  hashCode() method is used to compute the integer values. (The class must have a proper equals() implemented.)  You can form hash sets holding objects of type String, Integer, Double, Point, Rectangle or Color in standard library.  HashSet, HashSet >  HashSet (BankAccount should have equals() and hashCode() methods implemented) 6

 HashSet, you should provide hashCode() and equals() methods for Book Class.  Can I inherit the hashCode() and equals() methods from Object Class? ◦ If all elements are distinct, you can inherit the hashCode() and equals() of the Object class 7

 TreeSet uses binary search tree for arrange its elements, and elements are kept in sorted order.  Elements are stored in nodes as in a linked list which in a tree shape.  Use TreeSet for classes which implement Comparable Interface (to compare which node is larger)  If you want to visit the set’s element in sorted order, you should choose a TreeSet.  Set names = new TreeSet ()  Set names = new HashSet () 8

 Sets don’t have duplicates. Adding a duplicate of an element that is already present is ignored.  Removing an element that is not in the set is ignored too.  To use contains() method that you need define the equals() method. 9

10

 A set iterator visits the elements in the order in which the set implementation keeps them.  In HashSet, elements are visited in random order.  In TreeSet, elements are visited in sorted order even you inserted them in different order. 11

 ListIterator has an add() method to add an element at the list iterator position.  Iterator interface has no such method.  Removing element at iterator’s position for both ListIerator and Iterator.  Iterator has no previous() method, but ListIerator has. 12

 Read all words from dictionary, and put them in a set.  Read all words from a book, and put them in other set.  Print the words that are not in dictionary, which is potential misspelling. 13

14

15

16

 A map keeps associations between key and value objects. 17

 A map allows you to associate elements from a key set with elements from a value collection.  You use a map when you want to look up objects by using a key.  HashMap and TreeMap classes both implements the Map Interface. 18

19

 Enumerate all keys in a map.  Set name = scores.keySet(); for(String key: name){ Integer grade = scores.get(name); System.out.println(name + “->” + grade); } 20

21

22

 Due date: March 18 th.  Create a program that reads a text file and prints a list of all words in the file in alphabetical order, together with a count that indicates how often each word occurred in the file.  What collection should you use for these problem? 23