If you come from a Java background and looking for a HashMap equivalent in Python, then you can make use of the dictionary (dict) data structure in Python.
Example in Java
import java.util.HashMap;
public class HashMapJavaExample {
public static void main(String[] args) {
// Creating a HashMap
HashMap<String, Integer> map = new HashMap<>();
// Adding entries
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
// Accessing values
int value = map.get("B");
System.out.println(value); // Output: 2
// Removing an entry
map.remove("A");
// Checking existence
boolean containsKey = map.containsKey("A");
System.out.println(containsKey); // Output: false
// Size of the map
int size = map.size();
System.out.println(size); // Output: 2
}
}
Same Example in Python
# Creating a dictionary
dictionary = {
"A": 1,
"B": 2,
"C": 3
}
# Adding entries
dictionary["D"] = 4
# Accessing values
value = dictionary["B"]
print(value) # Output: 2
# Removing an entry
del dictionary["A"]
# Checking existence
containsKey = "A" in dictionary
print(containsKey) # Output: False
# Size of the dictionary
size = len(dictionary)
print(size) # Output: 3
Let's see a comparison in a form of a table.
Feature | Python Dictionary | Java HashMap |
---|---|---|
Declaration | my_dict = {} or my_dict = dict() | HashMap<KeyType, ValueType> map = new HashMap<>(); |
Add an Entry | my_dict[key] = value | map.put(key, value); |
Remove an Entry | del my_dict[key] | map.remove(key); |
Accessing a Value | value = my_dict[key] | value = map.get(key); |
Checking Existence | key in my_dict | map.containsKey(key) |
Size | len(my_dict) | map.size() |
Iterating Over Keys | for key in my_dict: | for (KeyType key : map.keySet()) { ... } |
Iterating Over Values | for value in my_dict.values(): | for (ValueType value : map.values()) { ... } |
Iterating Over Items | for key, value in my_dict.items(): | for (Map.Entry<KeyType, ValueType> entry : map.entrySet()) { ... } |
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python,
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
More Posts:
- connection.url property value in hibernate.cfg.xml for mysql - Java
- How to filter null values using Java 8 Stream API - Java
- What is the Default Admin user and Password for Jenkins - Linux
- How to see Storage Information on macOS Ventura - MacOS
- How to install Python 3.9 using brew on Mac - Python
- Simple Login Page using Bootstrap - Bootstrap
- How to change order of fields on new item form for SharePoint Online list - SharePoint
- [Fix] SharePoint: This embed code doesnt seem to work. We only support iframe based embeds - SharePoint