JCF Hashmap & Hashset Winter 2005 CS-2851 Dr. Mark L. Hornick.

Slides:



Advertisements
Similar presentations
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Advertisements

Appendix I Hashing. Chapter Scope Hashing, conceptually Using hashes to solve problems Hash implementations Java Foundations, 3rd Edition, Lewis/DePasquale/Chase21.
© 2004 Goodrich, Tamassia Hash Tables1  
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Maps, Dictionaries, Hashtables
Sets and Maps ITEC200 – Week Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn.
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.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 8.
Java Collection Framework. Interface Collection add(o) Add a new element clear() Remove all elements contains(o) Membership checking. IsEmpty() Whether.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
Java Collections Framework A presentation by Eric Fabricant.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
The Java Collections Framework (JCF) Introduction and review 1.
D ESIGN & A NALYSIS OF A LGORITHM 07 – M AP Informatics Department Parahyangan Catholic University.
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.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Hash Tables1   © 2010 Goodrich, Tamassia.
LECTURE 36: DICTIONARY CSC 212 – Data Structures.
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
Hashing Hashing is another method for sorting and searching data.
© 2004 Goodrich, Tamassia Hash Tables1  
CS201: Data Structures and Discrete Mathematics I Hash Table.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Computer Science 112 Fundamentals of Programming II Implementation Strategies for Unordered Collections.
CSC 212 – Data Structures Lecture 26: Hash Tables.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Maps Nick Mouriski.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
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.
CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation.
Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Sets and Maps Chapter 9.
Design & Analysis of Algorithm Map
Hashing & HashMaps CS-2851 Dr. Mark L. Hornick.
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
Hashing CSE 2011 Winter July 2018.
© 2013 Goodrich, Tamassia, Goldwasser
Computer Science 112 Fundamentals of Programming II
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Efficiency add remove find unsorted array O(1) O(n) sorted array
CS240: Advanced Programming Concepts
Data Structures and Database Applications Hashing and Hashtables in C#
Road Map CS Concepts Data Structures Java Language Java Collections
Hashing II CS2110 Spring 2018.
Data Structures and Database Applications Hashing and Hashtables in C#
Hashing CS2110.
Chapter 10 Hashing.
CSE 373: Data Structures and Algorithms
Maps.
14.1 The java.util Package.
"He's off the map!" - Eternal Sunshine of the Spotless Mind
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.
Hash Tables Computer Science and Engineering
Hash Tables Computer Science and Engineering
CSE 1020: The Collection Framework
Sets and Maps Chapter 9.
CSE 373 Separate chaining; hash codes; hash maps
Parsing JSON, Using Libraries, Java Collections, Generics
Chapter 8 The Map ADT.
slides created by Marty Stepp
Hashing in java.util
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Trees in java.util A set is an object that stores unique elements
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Lecture-Hashing.
Presentation transcript:

JCF Hashmap & Hashset Winter 2005 CS-2851 Dr. Mark L. Hornick

JCF HashMap stores <Key, Value> pairs in a hash table Example: HashMap<String, Student> students; //String is the key //Student is the value Key/Value pairs are stored in a Map.Entry object public class HashMap<K, V> implements Map<K, V> extends AbstractMap<K, V> Winter 2005 CS-2851 Dr. Mark L. Hornick

JCF HashMap Implements methods such as put(k,v) – insert value v with key k into the table get(k) – return the value associated with key k remove(k) – remove entry associated with key k containsKey(k) – search for entry with key k containsValue(v) – search for entry with value v size() clear() Winter 2005 CS-2851 Dr. Mark L. Hornick

HashMap’s advantage is overall performance Specifically, performance for the following operations is O(k); that is, constant time: put(k,v) – insert value v with key k into the table get(k) – return the value associated with key k remove(k) – remove entry associated with key k containsKey(k) – search for entry with key k Winter 2005 CS-2851 Dr. Mark L. Hornick

Performance is gained in exchange for memory utilization efficiency Hash tables map keys to table indices Some keys map to the same table index Some indices remain unmapped Winter 2005 CS-2851 Dr. Mark L. Hornick

The JCF HashMap collision handling mechanism At index 873 in the table, store the linked list of all elements whose keys hash to 873 This is called chaining It implements a simple singly-linked list Winter 2005 CS-2851 Dr. Mark L. Hornick

As chains get long, performance degrades to O(M) M is the number of entries chained together To maintain good performance, once a JCF HashMap becomes 75% full, it is resized All indices are recalculated Chains are removed or reduced Winter 2005 CS-2851 Dr. Mark L. Hornick

Winter 2005 CS-2851 Dr. Mark L. Hornick

Winter 2005 CS-2851 Dr. Mark L. Hornick

Another collision handler In Open Address hashing, when a collision occurs, the next available index is used to store the key/value This leads to some interesting practical implementation problems (see the text) Winter 2005 CS-2851 Dr. Mark L. Hornick

HashSets Winter 2005 CS-2851 Dr. Mark L. Hornick

A HashSet is an unordered Collection in which the Value is also the Key All Values in a HashSet must be unique Since Values are the Keys The HashSet class has all of the methods in the Collection interface (rather than Map) add, remove, size, contains, … plus toString (inherited from AbstractCollection) Winter 2005 CS-2851 Dr. Mark L. Hornick