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:
- Java HashMap equivalent in Python using dict data structure - Python
- How to Show File Extensions for Xcode Files - MacOS
- How to hide quick launch in SharePoint classic site - SharePoint
- Where are Notepad++ macros stored? - NotepadPlusPlus
- How to Scan iPhone for Virus? Is Antivirus it really required? - HowTos
- SharePoint Server 2016 IT Preview Deprecated Removed features - SharePoint
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver - Java
- clear is not recognized as an internal or external command operable program or batch file. - DOS