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!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- MySQL Workbench - Connection Warning - Incompatible/nonstandard server version or connection protocol detected - MySQL
- Android Alert Dialog with Checkboxes example - Android
- How to Exit a Loop in Python Code - Python
- How to install Anaconda on Mac (M1/M2 Mac) - Python
- Bash: Command to Find the Length of a String - Bash
- Python: Print Exception Stack trace like Java - Python
- How choose alternate Tab Bar icon in Notepad++ - NotepadPlusPlus
- Notepad++ Convert text from lower to upper case - NotepadPlusPlus