13. Hashing, HashMap and HashSet

Java HashCode

Integer itr = Integer.valueOf(17683); itr.hashCode(); // Output: 17683 String str = new String("17683"); str.hashCode(); // Output: s.charAt(0) * 31^(n-1) + s.charAt(1) * 31^(n-2) + ... + s.charAt(n-1)

Why Base 31?

ASCII Codes

A-Z (65-90), a-z (97-122)

Comparison with Base 32

Base 32

Base 31

hash() and indexFor()

In Java 7's HashMap:

int hash = hash(key.hashCode()); int i = indexFor(hash, table.length);

indexFor()

static int indexFor(int h, int length) { return h & (length - 1); }

Reason:

Example:

1010110 <- 86 AND 0000111 <- 7 ---------- 0000110 <- 6

Drawbacks:

  1. Drop the signed bit (Always positive number)
  2. Ignore higher-order bits (Not using all the data -> Collisions)

hash()

int hash = hash(key.hashCode());

HashMap Example

Map<String, Integer> freqOfWords = new HashMap<String, Integer>(10, 0.65f); String[] words = "coming together is a beginning keeping together is progress working together is success" .split(" "); for (String word : words) { Integer frequency = freqOfWords.get(word); if (frequency == null) { frequency = 1; } else { frequency++; } freqOfWords.put(word, frequency); }
Iterator<String> itr = freqOfWords.keySet().iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } System.out.println(); // using enhanced for-loop and keySet method for (String word : freqOfWords.keySet()) { System.out.println(word); } System.out.println(); // using enhanced for-loop and values method for (Integer frequency : freqOfWords.values()) { System.out.println(frequency); }

HashSet Example

Use of dummy object seems a waste of memory?

public class HashSet<E> implements Set<E> { private ... HashMap<E, Object> map; // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object(); public HashSet() { map = new HashMap<>(); } public boolean add(E e) { return map.put(e, PRESENT)==null; } }
Set<String> distinctWords = new HashSet<String>(); String[] words = "coming together is a beginning keeping together is progress working together is success" .split(" "); for (String word : words) { // No duplicate is allowed distinctWords.add(word); }

Back to Home Next Lecture