How To Create a Dictionary In Java
Kodebae
Posted on February 26, 2021
What Is A Java Dictionary?
The ORACLE definition is as follows:
"The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. Every key and every value is an object. In any one Dictionary object, every key is associated with at most one value. Given a Dictionary and a key, the associated element can be looked up. Any non-null object can be used as a key and as a value."
When you think about everyday (book-like) dictionaries, what comes to mind? How do you use normal dictionaries when looking stuff up? Dictionaries allow us to look up the meanings of words that we may or may not fully understand. So how do dictionaries function in Java? The exact same way actually. We are able to access the stored values of specific keys within our Java dictionaries. Woah, did I lose you? Think about it like this...
Our word that we want to know the meaning of is our 'key' in Java. The meaning of that word or the definition is our 'value'. In Java, we are able to store information with something called 'key: value pairs' and these key-value pairs are directly reminiscent of a dictionary. In this tutorial, I am going to show you how I created an English to German dictionary in Java. I will leave links to my GitHub repo and other important resources below. Let's get coding.
Project Setup
We need to create our dictionary using a HashMap and then we will need to set it up to hold both English and German string values. Think about what kind of values your dictionary needs to hold. Don't forget to import the HashMap class and the Map interface. We can do all of that like this...
How to create a Java dictionary
import java.util.HashMap;
import java.util.Map;
// This program is an English to German dictionary created in Java utilizing the abstract Class "Dictionary" using a HashMap
public class dictionary {
public static void main(String[] args) {
// English to German Dictionary
Map<String,String> englishToGermanDictionary = new HashMap<String,String>(); // creating a dictionary and setting both keys and values to String, could use any data type here.
}
}
Adding Values To The Dictionary
Now that we have our project instantiated let's add some key-value pairs. Since I am using the dictionary Class to create a literal English to German dictionary my key-value pairs will be strings with both English and German phrases. We can do all of that, like this.
Creating key-value pairs in Java
import java.util.HashMap;
import java.util.Map;
// This program is an English to German dictionary created in Java utilizing the abstract Class "Dictionary" using a HashMap
public class dictionary {
public static void main(String[] args) {
// English to German Dictionary
Map<String,String> englishToGermanDictionary = new HashMap<String,String>(); // creating a dictionary and setting both keys and values to String, could use any data type here.
englishToGermanDictionary.put("I'd like to practice German.", "Ich mochte Deutsch uben."); // adding English to German Strings to the dictionary
englishToGermanDictionary.put("Could you repeat that?", "Konnten Sie das bitte wiederholen?");
englishToGermanDictionary.put("Do you speak English?", "Sprechen Sie English?");
englishToGermanDictionary.put("Where is the bus stop?", "Wo ist die Bushaltestelle?");
englishToGermanDictionary.put("How much is this?", "Wie viel kostet das?");
englishToGermanDictionary.put("Can I try this on?", "Kann ich es anprobieren?");
englishToGermanDictionary.put("Could you take a photo of me?", "Konnten Sie ein Foto von mir machen?");
englishToGermanDictionary.put("My name is ...", "Mine Name ist...");
englishToGermanDictionary.put("Nice to meet you.", "Angenehm.");
}
}
Getting Values From The Dictionary
Now that we have all of our strings input into the dictionary how do we get them out? I'm so glad you asked. Good ol 'sout' to the rescue! We could also just print all of the key, or maybe all of the values. Ok, there are a few ways to get values out of the dictionary actually. So I decided to demonstrate a few below.
Getting Keys and Values Out Of A Dictionary
package ArrayStringPractice;
import java.util.HashMap;
import java.util.Map;
// This program is an English to German dictionary created in Java utilizing the abstract Class "Dictionary" using a HashMap
public class dictionary {
public static void main(String[] args) {
// English to German Dictionary
Map englishToGermanDictionary = new HashMap(); // creating a dictionary and setting both keys and values to String, could use any data type here.
englishToGermanDictionary.put("I'd like to practice German.", "Ich mochte Deutsch uben."); // adding English to German Strings to the dictioonary
englishToGermanDictionary.put("Could you repeat that?", "Konnten Sie das bitte wiederholen?");
englishToGermanDictionary.put("Do you speak English?", "Sprechen Sie English?");
englishToGermanDictionary.put("Where is the bus stop?", "Wo ist die Bushaltestelle?");
englishToGermanDictionary.put("How much is this?", "Wie viel kostet das?");
englishToGermanDictionary.put("Can I try this on?", "Kann ich es anprobieren?");
englishToGermanDictionary.put("Could you take a photo of me?", "Konnten Sie ein Foto von mir machen?");
englishToGermanDictionary.put("My name is ...", "Mine Name ist...");
englishToGermanDictionary.put("Nice to meet you.", "Angenehm.");
// Retrieve the values by acessing the keys
System.out.println(englishToGermanDictionary.get("I'd like to practice German.")); // using the keys to access the German translation
System.out.println(englishToGermanDictionary.get("Could you repeat that?"));
System.out.println(englishToGermanDictionary.get("Do you speak English?"));
System.out.println(englishToGermanDictionary.get("Where is the bus stop?"));
System.out.println(englishToGermanDictionary.get("How much is this?"));
System.out.println(englishToGermanDictionary.get("Can I try this on?"));
System.out.println(englishToGermanDictionary.get("Could you take a photo of me?"));
System.out.println(englishToGermanDictionary.get("My name is ..."));
System.out.println(englishToGermanDictionary.get("Nice to meet you."));
System.out.println(englishToGermanDictionary.keySet()); // will print out all of the keys
System.out.println(englishToGermanDictionary.values()); // will print out all values
}
}
<br>
### What's So Great About A Dictionary
Alright, that we have seen exactly how the Java dictionary works and how to use it (on a very basic level), I bet you're wondering what the big deal is. There are **several advantages to using a HashMap over an Array.** For one thing, arrays have a fixed size and HashMaps do not. So we could in fact just keep adding values to this HashMap. Lookup is easy as long as we have the key, and in most cases is 0(1) or constant time, worst case 0(n). Another good thing is that most data types can be used. That means we could use int or even booleans with our HashMaps as well. So what are the **disadvantages?** There are a few, we actually can not control what order the keys are stored in. With arrays, our data would be indexed and easy to look up. All we would need is the index. But with a HashMap we actually have no idea where the info is going to be stored. I linked an article below that explains how **HashMaps are actually built on top of arrays.**
### Conclusion
There you have it. Dictionaries are easy and simple and can be implemented to store key-value pairs. The keys and values can be accessed in different ways depending on what we need them to do. Please don't confuse HashMaps with HashTables in Java because they are in fact, different. I'll leave a link down below to an article that explains that in more detail. Well, that's it. What do you think of my English to German dictionary in Java? Leave me a comment or a <3.
Goodbye, Take care.
Kodebae ;)
**Credits:**
Author: 👩🏽💻 Kodebae
Buy me a ☕️: (https://www.buymeacoffee.com/karmendurbin)
Website: (https://karmen-durbin-swe.netlify.app/)
X: (https://twitter.com/karmen_durbin)
### Links --->
> ORACLE Docs
https://docs.oracle.com/javase/8/docs/api/java/util/Dictionary.html
> My GitHub
https://github.com/kodebae/Data-Structures-And-Algorithms-In-Java/blob/main/src/ArrayStringPractice/dictionary.java
> HashMaps are built on Arrays?
https://www.interviewcake.com/concept/java/hash-map
> Difference between HashMaps and HashTables
https://www.geeksforgeeks.org/hashmap-treemap-java/
> A video Explaining HashMaps
https://www.youtube.com/watch?v=c3RVW3KGIIE
Posted on February 26, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.