Docsity

Java Object-Oriented Programming II: Sets, Maps, and Hashing - Prof. Nelson Padua-Perez, Study notes of Computer Science

Partial preview of the text

Download Java Object-Oriented Programming II: Sets, Maps, and Hashing - Prof. Nelson Padua-Perez and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Sets, Maps, Hashing

Department of Computer Science

University of Maryland, College Park

Overview

Sets Maps Hashing

Scattering Hash Values Hash Function

Hash Tables

Open Addressing Chaining

Java equals and hashCode( )

Sets Set B Set C Set A

Properties

Collection of elementswithout duplicates No ordering (i.e., no frontor back) Order in which elementsadded doesn’t matter

Implementation goal

Offer the ability to find /remove element quickly Without searchingthrough all elements

How Do Sets Work in Java?

Finding matching element is based on equals( ) To build a collection for a class

Need to define your own equals(Object) method Default equals( ) uses reference comparison I.e., a.equals(b)

Æ

a == b a, b equal only if reference to same object Many classes have predefined equals( ) methods Integer.equals( )

Æ

compares value of integer String.equals( )

Æ

compares text of string

Map Definition

Map (associative array)

Unordered collection of keys For each key, an associated object Can use key to retrieve object

Can view as array indexed by any (key) value

Example A[“key1”] = … key1 key2 key3 key

Map Interface Methods

Methods

void put( Key, Object ) // inserts element Object get( Key ) // returns element void remove( Key ) // removes element Boolean containsKey( Key ) // looks for key Set keySet( ) // entire set of keys

Map Implementation

Implementation approaches

Two parallel arrays Unsorted Sorted Linked list Binary search tree Hash table

Java Collections Framework

TreeMap

uses red-black (balanced) tree HashMap

uses hash table

Java Collections Map Hierarchy Map SortedMap AbstractMap TreeMap HashMap LinkedHashMap

Hashing

Hash Table

Array indexed using hash values Hash table A with size N Indices of A range from 0 to N-1 Store in A[ hashValue % N]

Hash Function

Function for converting key into hash value For Java

Hash value

32-bit signed int Default hash function

int hashCode( )

For hash table of size N

Must reduce hash value to 0..N – 1 Can use modulo operator Math.abs(hash value % N)

Scattering Hash Values

Multiplicative congruency method

Produces good hash values Hash value = Math.abs((a * key.hashCode( )) % N) Where N is table size a is large prime number

Beware of % (Modulo Operator)

The % operator is integer remainder

x % y == x – y * ( x / y )

Result may be negative

–|y| < x % y < +|y|

x % y has same sign as x

Use Math.abs( x % N ), not Math.abs(x) % N

Since Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE! Will happen 1 in 2 32 times (on average) for random int values

Hash Function

Example hashCode("apple") = 5 hashCode("watermelon") = 3 hashCode("grapes") = 8 hashCode("kiwi") = 0 hashCode("strawberry") = 9 hashCode("mango") = 6 hashCode("banana") = 2 Perfect hash function

Unique values for each key

kiwi

banana

watermelon

apple

mango^ grapes

strawberry

0 1 2 3 4 5 6 7 8 9

Hash Function

Suppose nowhashCode("apple") = 5hashCode("watermelon") = 3hashCode("grapes") = 8hashCode("kiwi") = 0hashCode("strawberry") = 9hashCode("mango") = 6hashCode("banana") = 2hashCode(“orange") = 3 Collision

Same hash value formultiple keys

kiwi

banana

watermelon

apple

mango^ grapes

strawberry

0 1 2 3 4 5 6 7 8 9