// Java Hash Map
// -------------
import java.util.HashMap;
public class Hash_Map {
public static void main(String[] args) {
HashMap<String, String> countries = new HashMap<>();
// Inserting values
countries.put("Turkey", "Ankara");
countries.put("USA", "Washington, D.C.");
countries.put("Russia", "Moscow");
countries.put("China", "Beijing");
countries.put("India", "New Dehli");
// Printing the entire HashMap
System.out.println(countries);
System.out.println();
// Removing items in Hashmap
countries.remove("India");
// Printing the entire HashMap
System.out.println(countries);
System.out.println();
// Finding and Printing specific item in Hashmap
System.out.println(countries.get("Russia")); // this will print its value
System.out.println();
// Printing the Size of HashMap
System.out.println("Size: " + countries.size());
System.out.println();
// Replacing Values in Hashmap
countries.replace("USA", "Detroit");
// Printing the entire HashMap after replacing value
System.out.println(countries);
System.out.println();
// Checking for some key's existence in HashMap and Printing the result
System.out.println(countries.containsKey("Pakistan"));
System.out.println();
// Checking for some value's existence in HashMap and Printing the result
System.out.println(countries.containsValue("Beijing"));
System.out.println();
// Printing the entire Hasmap Line by Line
for (String i : countries.keySet()) {
System.out.print(i + "=");
System.out.println(countries.get(i));
}
System.out.println();
// Delete All values in HashMap And Printing To see Result
countries.clear();
System.out.println(countries);
}
}
In computing, a hash table (hash map) is a data structure that implements an
associative array abstract data type, a structure that can map keys to values.
A hash table uses a hash function to compute an index, also called a hash code,
into an array of buckets or slots, from which the desired value can be found.
HashMap hm=new HashMap(int initialCapacity, float loadFactor);
import java.util.HashMap; // import the HashMap class
HashMap<String, String> capitalCities = new HashMap<String, String>();
int main() {
unordered_map<int, int> hashmap;
// 2. insert a new (key, value) pair
hashmap.insert(make_pair(0, 0));
hashmap.insert(make_pair(2, 3));
// 3. insert a new (key, value) pair or update the value of existed key
hashmap[1] = 1;
hashmap[1] = 2;
// 4. get the value of a specific key
cout << "The value of key 1 is: " << hashmap[1] << endl;
// 5. delete a key
hashmap.erase(2);
// 6. check if a key is in the hash map
if (hashmap.count(2) <= 0) {
cout << "Key 2 is not in the hash map." << endl;
}
// 7. get the size of the hash map
cout << "the size of hash map is: " << hashmap.size() << endl;
// 8. iterate the hash map
for (auto it = hashmap.begin(); it != hashmap.end(); ++it) {
cout << "(" << it->first << "," << it->second << ") ";
}
cout << "are in the hash map." << endl;
// 9. clear the hash map
hashmap.clear();
// 10. check if the hash map is empty
if (hashmap.empty()) {
cout << "hash map is empty now!" << endl;
}
}