HomeSearch

C# HashMap (Dictionary, Java)

This C# example page uses a Dictionary to get the functionality of a Hashmap from Java.
HashMap. For C# programs, we do not use a HashMap as a collection of key-value pairs. Instead we use a Dictionary. Java programs use HashMap, and this functionality can be translated to C#.
Methods, notes. The put() method on HashMap can be changed to the Add() method on Dictionary. And we can use TryGetValue instead of getOrElse.
Example program. To begin, we have a program in C# that adds 3 string keys with 3 int values to a Dictionary. We get one of the values. Then we try to get a value that does not exist.Dictionary

Loop: We use a foreach-loop over the Dictionary to iterate the KeyValuePairs. A KeyValuePair is similar to a Java Entry.

C# program that uses Dictionary using System; using System.Collections.Generic; class Program { static void Main() { // Create Dictionary. Dictionary<string, int> hash = new Dictionary<string, int>(); // Add some data. hash.Add("diamond", 500); hash.Add("ruby", 200); hash.Add("pearl", 100); // Get value that exists. int value1 = hash["diamond"]; Console.WriteLine("get DIAMOND: " + value1); // Get value that does not exist. hash.TryGetValue("coal", out int value2); Console.WriteLine("get COAL: " + value2); // Loop over items in collection. foreach (KeyValuePair<string, int> pair in hash) { Console.WriteLine("KEY: " + pair.Key); Console.WriteLine("VALUE: " + pair.Value); } } } Output get DIAMOND: 500 get COAL: 0 KEY: diamond VALUE: 500 KEY: ruby VALUE: 200 KEY: pearl VALUE: 100
Java HashMap. For comparison, here is a Java program that creates a HashMap. It adds 3 string keys with 3 Integer values to the collection. It uses get() and getOrElse() to access values.

Loop: For the loop, it uses for-loop over the collection returned by entrySet(). We iterate over each Entry.

Java program that uses HashMap import java.util.HashMap; import java.util.Map.Entry; public class Program { public static void main(String[] args) { // New HashMap. HashMap<String, Integer> hash = new HashMap<>(); // Add to collection. hash.put("diamond", 500); hash.put("ruby", 200); hash.put("pearl", 100); // Get values from collection. int value1 = hash.get("diamond"); System.out.println("get DIAMOND: " + value1); // Try to get a key that does not exist. int value2 = hash.getOrDefault("coal", -1); System.out.println("get COAL: " + value2); // Display entire HashMap. for (Entry<String, Integer> entry : hash.entrySet()) { System.out.println("KEY: " + entry.getKey()); System.out.println("VALUE: " + entry.getValue()); } } } Output get DIAMOND: 500 get COAL: -1 KEY: diamond VALUE: 500 KEY: ruby VALUE: 200 KEY: pearl VALUE: 100
Notes, language comparison. We can see the Dictionary and HashMap are almost exactly the same collection. They have different names, and the syntax is a bit different.

However: On a conceptual level they are equivalent. We can use either with just some syntax adjustments.

A summary. Typically no HashMap is used in C#. And in Java we do not use a "Dictionary" as often. But the collections has the same purpose, and can be used in almost the same way.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls