Home
Map
HashMap (Dictionary, Java)Use a Dictionary to get the functionality of a Hashmap from Java.
C#
This page was last reviewed on Dec 15, 2021.
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#.
Shows a dictionary
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
Detail We use a foreach-loop over the Dictionary to iterate the KeyValuePairs. A KeyValuePair is similar to a Java Entry.
foreach
KeyValuePair
Shows a 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); } } }
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.
Detail For the loop, it uses for-loop over the collection returned by entrySet(). We iterate over each Entry.
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()); } } }
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 15, 2021 (edit link).
Home
Changes
© 2007-2024 Sam Allen.