By Kevin Hou
4 minute read
Recently in class, I’ve been learning a lot about different data structures to use in programs. Many of the common, most applicable, include linked lists, stacks, queues, double linked lists, and symbol tables. In this post I will give a brief overview of symbol tables, also known as dictionaries or hash maps, and provide some instances where symbol tables can greatly improve the quality of your code. When implemented correctly, this simple data type can improve efficiency, code readability, and run time.
In a nutshell, a dictionary is a data type where a key and a value are matched. In order to access a value, you provide a key. To store a value, you map a key to a value. To cycle through all values in the array, you again must use keys. The easiest analogy for understanding how a dictionary works is to use just that: a dictionary.
At it’s most basic form, a dictionary is a database of words that are assigned a definition. To find the meaning of a word, you must first look up the word. The word and its definition serve as the key and its value, respectively. In code, the value can be any data type. In some languages (i.e. Java), the data type is immutable and consistent across all values. In other languages (i.e. Javascript), the data type can vary from key to key and from word to word.
In both cases described above, both the key and the value can take on any data type. For example, the analogy of a dictionary describes Strings mapped to Strings; however, that’s not always the case. For example, the best data structure for storing birthdays, is a String (the person’s name) mapped to a date type (the person’s birthday). For a frequency table of letters, the letter can be either a character, integer, or String and its values take on the form of integers.
Javascript:
1var dictionary = []; // Create an empty array
2dictionary.push({
3 // Add an value to the dictionary
4 key: "keyName",
5 value: "the value",
6});
7
Swift:
1let dictionary = [ // New dictionary
2 "keyName" : "the value",
3 "keyName" : "the value"
4]
5dictionary.append("keyName" : "the value") // Add new item to dictionary
6
Java:
Source: arshajii at stackoverflow.com
1// Using hashmap 2Map<String, String> map = new HashMap<String, String>(); 3map.put("dog", "type of animal"); 4System.out.println(map.get("dog")); 5
Python:
1dict = { 2 'Name' : ‘Kevin’, 3 ‘School’. : ‘Princeton University’, 4 ‘Year’. : 1 5} 6print dict['Name'] # prints “Kevin” 7
I’ve found more and more use cases in the past couple of weeks for dictionaries. It’s often very useful when you have to store a potentially uncapped number of items that aren’t easily mapped to an index of an array. Unlike searching for items within traditional arrays which take linear time, dictionary calls are of constant time.
Sometimes, items either simply cannot be matched to an integer index or prove too complicated to map to an ordered list of integers. Instead, it can be much easier to call values based off of their keys.
Here are a variety of instances where they can used: