Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java hash map

// 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);

    }
}
Comment

hash map

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.
Comment

hash map java

HashMap hm=new HashMap(int initialCapacity, float loadFactor); 
Comment

hash map

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;
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: spring mvc 
Java :: run jar file with different jre 
Java :: list of list in java 
Java :: enum class in java 
Java :: how to remove an element from an arraylist java 
Java :: how to divide a double in java 
Java :: meter to cm 
Java :: java array 
Java :: creating a properties object using a file 
Java :: mcq java 
Java :: javafx 
Java :: deep content 
Java :: java evaluate two expressions in if statemenmt 
Java :: @Async how it works @EnableAsync 
Java :: how to change state of a Switch programmatically andoir dstudio 
Java :: java import keyword 
Java :: calculate mcd in javsa 
Java :: Android Studio First time run check 
Java :: syntax for new module creation in java 
Java :: what is java.io example 
Java :: longest subarray with equal 0 and 1 
Java :: java 8 lambda delete from list 
Java :: time complexity of indexof java 
Java :: java write number with variable decimal 
Java :: number guessing game in java using oops 
Java :: java requirenonnull 
Java :: import txt.xz file to android studio app 
Java :: media style dependency androidx 
Java :: isblank vs isempty java string utils 
Java :: java lambda expressions qunado foi implantada 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =