Hash Functions and the HashMap Class A Brief Overview On Green Marble John W. Benning.

Slides:



Advertisements
Similar presentations
Hash Tables and Sets Lecture 3. Sets A set is simply a collection of elements Unlike lists, elements are not ordered Very abstract, general concept with.
Advertisements

Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
CSCE 3400 Data Structures & Algorithm Analysis
Hashing as a Dictionary Implementation
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Nov 12, 2009IAT 8001 Hash Table Bucket Sort. Nov 12, 2009IAT 8002  An array in which items are not stored consecutively - their place of storage is calculated.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
Dictionaries and Hash Tables1  
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.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
1. 2 Problem RT&T is a large phone company, and they want to provide enhanced caller ID capability: –given a phone number, return the caller’s name –phone.
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:
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Hashing CS 105. Hashing Slide 2 Hashing - Introduction In a dictionary, if it can be arranged such that the key is also the index to the array that stores.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
1 HashTable. 2 Dictionary A collection of data that is accessed by “key” values –The keys may be ordered or unordered –Multiple key values may/may-not.
Hash Tables1   © 2010 Goodrich, Tamassia.
Chapter 18 Java Collections Framework
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Hashing Hashing is another method for sorting and searching data.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing as a Dictionary Implementation Chapter 19.
CS201: Data Structures and Discrete Mathematics I Hash Table.
CSC 427: Data Structures and Algorithm Analysis
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
Hashing CS 110: Data Structures and Algorithms First Semester,
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
A Introduction to Computing II Lecture 11: Hashtables Fall Session 2000.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
Copyright © Curt Hill Hashing A quick lookup strategy.
Dictionaries and Hashing CSCI 3333 Data Structures.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
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”
Building Java Programs Generics, hashing reading: 18.1.
Sets and Maps Chapter 9.
The need for Programming Languages
Week 8 - Wednesday CS221.
Efficiency add remove find unsorted array O(1) O(n) sorted array
Hash Tables.
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
Hashing CS2110 Spring 2018.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Sets and Maps Chapter 9.
Hashing in java.util
Collision Handling Collisions occur when different elements are mapped to the same cell.
Data Structures and Algorithm Analysis Hashing
Presentation transcript:

Hash Functions and the HashMap Class A Brief Overview On Green Marble John W. Benning

What are hash functions? (Online definition): A hash function (or hash algorithm) is a way of creating a small digital "fingerprint" from any kind of data. The function chops and mixes the data to create the fingerprint, often called a hash value or hash sum. The hash value is commonly represented as a short string of random-looking letters and numbers.

Hashing Fundamentals A fundamental property of all hash functions is that if two hashes (according to the same hash function) are different, then the two inputs are different in some way. On the other hand, the equality of two hash values suggests, but does not guarantee the equality of the two inputs. If a hash value is calculated for a piece of data, and then one bit of that data is changed, a hash function with strong mixing property usually produces a completely different hash value.

Applications of Hash Functions Cryptography- assumes the existence of an adversary who can deliberately try to find inputs with the same hash value. A well designed cryptographic hash function is a "one-way" operation: there is no practical way to calculate a particular data input that will result in a desired hash value, so it is also very difficult to forge. Hash Tables- enable fast lookup of a data record given its key. (Note: Keys are not usually secret as in cryptography, but both are used to "unlock" or access information.) For example, keys in an English dictionary would be English words, and their associated records would contain definitions. In this case, the hash function must map alphabetic strings to indexes for the hash table's internal array.

Applications of Hash Functions (cont.) Error Correction- Using a hash function to detect errors in transmission is straightforward. The hash function is computed for the data at the sender, and the value of this hash is sent with the data. The hash function is performed again at the receiving end, and if the hash values do not match, an error has occurred at some point during the transmission. This is called a redundancy check. Audio Identification- for audio identification such as finding out whether an MP3 file matches one of a list of known items

Hash Tables We will be looking at using hash algorithms to fill hash tables for data lookup. Below is an illustration of the use of a hash algorithm. Fox The red fox runs across the ice The red fox walks across the ice Hash Function Input Hash Sum Hash Function

Hash Tables Like arrays, hash tables provide constant- time O(1) lookup on average, regardless of the number of items in the table. However, the rare worst-case lookup time can be as bad as O(n). Compared to other associative array data structures, hash tables are most useful when large numbers of records of data are to be stored.

Choosing a hash function A good hash function is essential for good hash table performance. Hash collisions are generally resolved by some form of linear search, so if a hash function tends to produce similar values, slow searches will result. Because a good hash function can be hard to design, or computationally expensive to execute, much research has been devoted to collision resolution strategies that mitigate poor hashing performance. However, none of them is as effective as using a good hash function in the first place. It is desirable to use the same hash function for arrays of any conceivable size. To do this, the index into the hash table's array is generally calculated in two steps: -A generic hash value (hash sum) is calculated which fills a natural machine integer, -This value is reduced to a valid array index by finding its modulus* with the array’s size *modulus: found by calculating the difference between the hash sum and array size, and then finding an integer (the modulus) that, when divided into both numbers, gives the same remainder

HashMap Luckily, Java has a HashMap class. So we will be able to create hash tables without creating our own hash functions, though, if you want to create your own, you may be able to improve on Java’s hash algorithm

HashMap Constructors HashMapHashMap() Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75). HashMapHashMap(int initialCapacity) Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75). HashMapHashMap(int initialCapacity, float loadFactor) Constructs an empty HashMap with the specified initial capacity and load factor. HashMapHashMap(Map m) Constructs a new HashMap with the same mappings as the specified Map.Map *The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the capacity is roughly doubled by calling the rehash method.

Method Summary void clearclear() Removes all mappings from this map. Object cloneclone() Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned. boolean containsKeycontainsKey(Object key) Returns true if this map contains a mapping for the specified key.Object boolean containsValuecontainsValue(Object value) Returns true if this map maps one or more keys to the specified value.Object Set entrySetentrySet() Returns a collection view of the mappings contained in this map. Object getget(Object key) Returns the value to which the specified key is mapped in this identity hash map, or null if the map contains no mapping for this key.Object boolean isEmptyisEmpty() Returns true if this map contains no key-value mappings. Set keySetkeySet() Returns a set view of the keys contained in this map.

Object putput(Object key, Object value) Associates the specified value with the specified key in this map.Object void putAllputAll(Map m) Copies all of the mappings from the specified map to this map These mappings will replace any mappings that this map had for any of the keys currently in the specified map.Map Object removeremove(Object key) Removes the mapping for this key from this map if present.Object int sizesize() Returns the number of key-value mappings in this map. Collection valuesvalues() Returns a collection view of the values contained in this map.

Simple use of HashMap import java.util.HashMap; public class hashM { public static void main(String[] args) { HashMap k=new HashMap(); k.put("five",5); k.put("six",6); k.put("ten",10); System.out.println(k.containsKey("six")); System.out.println(k.containsKey("eleven")); System.out.println(k.containsValue(6)); System.out.println(k.get("ten")); System.out.println(k.size()); } *output true false true 10 3

HashMap Click here to go to the Java HashMap page onlineClick here to go to the Java HashMap page online