How to Use the Java Hashmap Data Structure Code With Example

Yowateh.id – Use the Java Hashmap. HashMap is quite possibly of the most proficient data structures. Store your key-esteem matches information utilizing the Java HashMap information structure.

A HashMap (or a HashTable) is an information structure that permits fast admittance to information utilizing key-esteem matches. The Java HashMap class broadens the AbstractMap class and carries out the Guide interface, which gives it admittance to a great deal of tasks. HashMaps have two sort boundaries — K and V, where K stores the keys and V stores the qualities in each HashMap.

The HashMap permits you to plan keys to a particular worth and store things in irregular request. In this article, you will figure out how to utilize Java’s HashMap class to store, recover, update, and erase information.

Creating a Java HashMap

Use the Java Hashmap Data Structure Code. The Java HashMap class has four constructors. The default constructor is the most famous one, it permits you to make a void HashMap that will have a default limit of 16.

import java.util.HashMap;

public class Main {

public static void main(String[] args) {

//creating a HashMap

HashMap<Integer, String> fruitsMap = new HashMap<Integer, String>();

}

}

The Java class above utilizes the default HashMap constructor to make an information structure called fruitsMap. The fruitsMap item will store information that has a number key and a string esteem. Nonetheless, since the HashMap class carries out the Guide interface, it can store an invalid worth in either the key field, the worth field, or both.

Storing Data in a HashMap

You can play out a few tasks utilizing the HashMap class. The put(K key, V worth) technique is among its most well known strategies, and it permits you to store information utilizing a key-esteem pair, where each critical guides to a worth.

import java.util.HashMap;
public class Main {
public static void main(String[] args) {
//creating a HashMap
HashMap<Integer, String> fruitsMap = new HashMap<Integer, String>();
//add items to HashMap
fruitsMap.put(3,"Pear");
fruitsMap.put(6,"Mango");
fruitsMap.put(7,"Plum");
fruitsMap.put(4,"Apple");
fruitsMap.put(1,"Orange");
fruitsMap.put(5,"Berries");
fruitsMap.put(2,"Melon");
}
}

The refreshed class above utilizes the put() strategy to add a few foods grown from the ground key to the current fruitsMap information structure. It is vital to haphazardly take note of that the code above adds the information to the guide. This detail will become helpful when you begin perusing the information from the HashMap.

Reading Data From a HashMap

There are multiple ways of perusing information from a HashMap. The technique that you use will rely heavily on how you believe the information should look or even the kind of information you need to recover.

Retrieve the HashMap Object

If you want to retrieve the data as an object, you can simply access the HashMap object, which in the example below is fruitsMap.

//View all the items as an object

System.out.println(fruitsMap);

Adding the line of code above to the Main class, prints the following output in the console:

{1=Orange, 2=Melon, 3=Pear, 4=Apple, 5=Berries, 6=Mango, 7=Plum}

Though each value was randomly added to the HashMap, from the output you will see that HashMap stores the values in ascending order, based on the key assigned to each value. This is a feature that puts HashMaps ahead of other data structures such as the Array data structure that only support linear storage.

Retrieve All the Data Individually

If you want to retrieve all the data individually, then you can use the for method that allows you to traverse through the HashMap and print each value and its corresponding key. This method employs the Map interface that the HashMap class implements.

//view all items with an iterator 
for (HashMap.Entry<Integer, String> fruit : fruitsMap.entrySet())
{
System.out.println("Key: " + fruit.getKey() + " Value: " + fruit.getValue());
}

Adding the method above to your code will print the following output in your console:

Key: 1 Value: Orange
Key: 2 Value: Melon
Key: 3 Value: Pear
Key: 4 Value: Apple
Key: 5 Value: Berries
Key: 6 Value: Mango
Key: 7 Value: Plum

Retrieve a Specific Value

The HashMap class has a get() method that takes a key and returns the value mapped to this key.

//fetch a single item
System.out.println(fruitsMap.get(4));

The line of code above prints the following output in the console:

Apple

Updating Data in a HashMap

After you create and populate a HashMap object, you can use the replace() method to update any item in the Map. The replace() method takes two or three arguments. The first replace() method takes the key associated with an existing item, along with the new value you want to map to it.

// replace a single item
fruitsMap.replace(4, "Grapes");
System.out.print(fruitsMap);

Executing the code above prints the following object in the console:

{1=Orange, 2=Melon, 3=Pear, 4=Grapes, 5=Berries, 6=Mango, 7=Plum}

As you can see from the object above, “Grapes” maps to 4, which previously mapped to “Apple”.

The second replace() method takes the key associated with the existing item, the existing item, and the replacement value.

// replace a single item
fruitsMap.replace(4, "Apple", "Grapes");
System.out.print(fruitsMap);

The code above prints the following object in the console:

{1=Orange, 2=Melon, 3=Pear, 4=Grapes, 5=Berries, 6=Mango, 7=Plum}

Deleting Data From a HashMap

You can either delete a specific item from your HashMap using the remove() method or delete all the data using the clear() method. The remove() method takes either one or two arguments. The first remove method takes the key associated with the value you want to remove:

 //delete a single item
fruitsMap.remove(5);
System.out.println(fruitsMap);

The code above prints the following object in the console:

{1=Orange, 2=Melon, 3=Pear, 4=Apple, 6=Mango, 7=Plum}

The updated object shows that the item with the key-value pair of 5 and “Berries” is no longer a part of the fruitsMap. The second remove() method takes a key and the value that it maps to.

//delete a single item
fruitsMap.remove(5, "Berries");
System.out.println(fruitsMap);

The code above also prints the following object in the console:

{1=Orange, 2=Melon, 3=Pear, 4=Apple, 6=Mango, 7=Plum}

The clear() method takes no arguments and returns void.

fruitsMap.clear();
System.out.println(fruitsMap);

Executing the code above prints the following empty object in the console:

{}

Now You Can Perform CRUD Operations on a Java HashMap

Use the Java Hashmap Data Structure Code With Example. HashMaps are one of several popular data structures that you need to know. This article teaches you how to perform CRUD operations on Java HashMaps. The HashMap is a very useful data structure, its main selling point is that it is one of the most efficient data structures, due to its access speed. However, it is also very convenient to use, as it allows random storage.

Presently You Can Perform Muck Procedure on a Java HashMap
Use the Java Hashmap Data Structure Code With Example. HashMaps are one of a few famous information structures that you want to be aware. This article shows you how to perform Muck procedure on Java HashMaps. The HashMap is an exceptionally helpful information structure, its primary selling point is that it is perhaps of the most effective datum structures, because of its entrance speed. Notwithstanding, it is additionally extremely advantageous to use, as it permits irregular capacity. Pinterest: Yowatech

Tinggalkan komentar