xxxxxxxxxx
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashhMap {
public static void main(String[] args){
Map<String, String> map = new HashMap<>();
map.put("myName","Kumaran");
map.put("myProgram", "Java");
map.put("Designation", "Java Developer");
Set<String> key = map.keySet();
for(String keys: key){
System.out.println(keys+ " : " +map.get(keys));
}
}
}
xxxxxxxxxx
package org.o7planning.tutorial.javacollection.helloworld;
import java.util.HashMap;
public class HelloHashMap {
public static void main(String[] args) {
// Créer un objet HashMap qui stocke les paires des ID d'employés et des salaires.
// Clé de chaîne: ID d'employé
// Valeur flottante: Salaire.
HashMap<String, Float> salaryMap = new HashMap<String, Float>();
salaryMap.put("E01", 1000f);
salaryMap.put("E02", 12000f);
salaryMap.put("E03", 12300f);
salaryMap.put("E04", 1000f);
salaryMap.put("E05", 300.5f);
// Obtenir les salaires des employés 'E02'
Float salary= salaryMap.get("E01");
System.out.println("Salary of employee E01 = "+ salary);
// Mettre à jour le salaire pour les employés 'E05'
salaryMap.put("E05", 400f);
System.out.println("Salary of employee E05 = "+ salaryMap.get("E05"));
}
}
xxxxxxxxxx
// Java program to illustrate HashMap class of java.util
// package
// Importing HashMap class
import java.util.HashMap;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create an empty hash map by declaring object
// of string and integer type
HashMap<String, Integer> map = new HashMap<>();
// Adding elements to the Map
// using standard put() method
map.put("vishal", 10);
map.put("sachin", 30);
map.put("vaibhav", 20);
// Print size and content of the Map
System.out.println("Size of map is:- "
+ map.size());
// Printing elements in object of Map
System.out.println(map);
// Checking if a key is present and if
// present, print value by passing
// random element
if (map.containsKey("vishal")) {
// Mapping
Integer a = map.get("vishal");
// Printing value for the corresponding key
System.out.println("value for key"
+ " \"vishal\" is:- " + a);
}
}
}
xxxxxxxxxx
import java.util.HashMap; // import the HashMap class
HashMap<String, String> capitalCities = new HashMap<String, String>();
xxxxxxxxxx
// Java program to illustrate HashMap class
// of java.util package
// Importing HashMap class
import java.util.HashMap;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create an empty hash map by declaring object
// of string and integer type
HashMap<String, Integer> map = new HashMap<>();
// Adding elements to the Map
// using standard put() method
map.put("vishal", 10);
map.put("sachin", 30);
map.put("vaibhav", 20);
// Print size and content of the Map
System.out.println("Size of map is:- "
+ map.size());
// Printing elements in object of Map
System.out.println(map);
// Checking if a key is present and if
// present, print value by passing
// random element
if (map.containsKey("vishal")) {
// Mapping
Integer a = map.get("vishal");
// Printing value for the corresponding key
System.out.println("value for key"
+ " \"vishal\" is:- " + a);
}
}
}
xxxxxxxxxx
private static final Map<Integer, String> map = Map.of(
1, "one",
2, "two"
);
xxxxxxxxxx
// hashMap creation with 8 capacity and 0.6 load factor
HashMap<K, V> numbers = new HashMap<>();
HashMap<String, Integer> numbers = new HashMap<>();
xxxxxxxxxx
#include <iostream>
#include <string>
#include <unordered_map>
int main()
{
// Create an unordered_map of three strings (that map to strings)
std::unordered_map<std::string, std::string> u = {
{"RED","#FF0000"},
{"GREEN","#00FF00"},
{"BLUE","#0000FF"}
};
// Helper lambda function to print key-value pairs
auto print_key_value = [](const auto& key, const auto& value) {
std::cout << "Key:[" << key << "] Value:[" << value << "]\n";
};
std::cout << "Iterate and print key-value pairs of unordered_map, being\n"
"explicit with their types:\n";
for( const std::pair<const std::string, std::string>& n : u ) {
print_key_value(n.first, n.second);
}
std::cout << "\nIterate and print key-value pairs using C++17 structured binding:\n";
for( const auto& [key, value] : u ) {
print_key_value(key, value);
}
// Add two new entries to the unordered_map
u["BLACK"] = "#000000";
u["WHITE"] = "#FFFFFF";
std::cout << "\nOutput values by key:\n"
"The HEX of color RED is:[" << u["RED"] << "]\n"
"The HEX of color BLACK is:[" << u["BLACK"] << "]\n\n";
std::cout << "Use operator[] with non-existent key to insert a new key-value pair:\n";
print_key_value("new_key", u["new_key"]);
std::cout << "\nIterate and print key-value pairs, using `auto`;\n"
"new_key is now one of the keys in the map:\n";
for( const auto& n : u ) {
print_key_value(n.first, n.second);
}
}