Files, Static and Final



Course Evaluations in Miverva

File Input/Output

Reading a text file

You need the FileReader and BufferedReader classes

Writing a text file

You need the FileWriter and BufferedWriter classes

Writing a text file

You need the FileWriter and BufferedWriter classes

The static and final keywords

Static

We use the static keyword when we want a variable or method to be accesible without creating an instance of a class

Static

We use the static keyword when we want a variable or method to be accesible without creating an instance of a class

Static

Some rules for using static methods and variables

Static + Final

The final keyword is used when we want the value of a static variable to be constant



This is usually useful when you want to precompute certain numbers and keep them constants during the execution of your program.

How a Hashtable works

Hashtable

Think of a Hashtable as a dictionary

We use a Hastable to associate a key to a value

Hashtable

Keys, as well as values, can be of any type

Hashtable

A hash table is a generalization of the simple fixed-size array

Hashtable

To get direct access, a hash table uses a fixed-size array to store data.

We would associate each key to a position in the array, and store the data in the corresponding slot in the array.

We could try and have one slot in the array for every possible key; i.e. map each possible name to a number in the array

This is not feasible if the number of keys is very big ( How many possible names are there?)

Furthermore, we might not even need all of the possible positions in the array to store data

Hashing

Instead, we use a fixed-size array with a small size. For example, we could use just 26 slots for all possible names in a phone book.

We need to compute an index for every key. This is called hashing .

Hashing function

A hashing function maps input data of arbitrary size to output data of fixed size; i.e. The set of all possible person names to 26 slots

For a key k, we store its value in the slot given by the hashing function f(k)

We say that f(k) is the hash value of the key k

A good hashing function would give different hash values to every key

Hashing function

But since we are limiting the size of the fixed-size array (the number of slots available) ...

.. multiple keys will have the same hash value. This is called a collision

Collisions in hashing functions

Collisions happen when more than two keys have the same hash value


We can resolve collisions by

Collisions in hashing functions

Collisions happen when more than two keys have the same hash value


We can resolve collisions by

Collisions in hashing functions

We can resolve collisions by

Picking a good hashing function is a good idea: In our example, there are a lot more people with a name starting with J, than there are people with a name starting with Q

But as long as there are more keys than available slots (hash values), there will be collisions.

Collisions in hashing functions

We can resolve collisions by

We lose some of the benefit of direct addressing. But this is: 1) faster than a Linked list on average 2) More flexible than a fixed-size array

An implementation of a HashTable

See the provided code in the course website

An exercise for you:

Download the baby-names.csv file from the course website

Resources

/