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

Java HashMap

import java.util.HashMap; // import the HashMap class

HashMap<String, String> capitalCities = new HashMap<String, String>();
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 :: how to create textview in java android 
Java :: what is java 
Java :: java int 0/0 
Java :: convert string to int in java 
Java :: exception class implementation in java 
Java :: convert alphabet to number in java 
Java :: multithreading in java 
Java :: java switch tutorial 
Java :: java linked list swap elements 
Java :: look and feel java 
Java :: java how to split a string to array 
Java :: java creating an object 
Java :: java how to create subclass 
Java :: recyclerview adapter multiple view types 
Java :: switch statement in java 
Java :: knapsack problem 
Java :: How to perform in-order traversal of a binary tree? 
Java :: Java Create a PrintWriter 
Java :: why we use return method 
Java :: Java Abstract Class and Method 
Java :: super class java 
Java :: pass data to layout from navigation android studio 
Java :: calculate the area of two squares in java by using a method 
Java :: exoplayer how to put loader while video is still loading android java 
Java :: create variables in java 
Java :: java inser at index 
Java :: java.lang.NullPointerException: Cannot store to double array because is null 
Java :: What is the use of @Listener annotation in TestNG? 
Java :: java 8 lambda delete from list 
Java :: cgange background from button click java fx 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =