Verification: 52bc5e43554438b2
Java

How to add values using hashmap put() method in java using Netbeans

Description:

How to add values using hashmap put() method in java- in this article, I am going to show you how to use Hashmap put method in java with programming examples.

When using Java, it is essential to handle collections such as List and Map.

However, Maps are slightly different than Lists in how values ​​are added. In this article, I will explain how to add values ​​to the Map with the following contents.

  • What is Map?
  • How to add value using hashMap put() method?
  • Basic usage of the hashmap put() method in java
  • How to add a value to a List or an array?
  • How to add values ​​to a Map in bulk?
  • How to use the hashamap putAll() method in java
  • How to get the value of a Map?




What is Map

The java.util.Map interface is one of the collections that can hold values ​​associated with keys. A collection should be understood as a dynamic array that can be resized later.

The Map interface mainly uses java.util.HashMap and java.util.TreeMap. 

Basic syntax of map objects creating:

//Create object of HashMap class

Map mapData = new HashMap();

// Create a HashMap class object by specifying a type argument

Map <data type, data type> map variable = new HashMap<data type, data type>();     

Below is a concrete example of generating a Map. Integer, String, List, Map, etc. can be specified as the value to be stored.

// Specify String type

Map<String, String>mapData = new HashMap<String, String>();

//Specify List

Map<String, List<String>>mapData = new HashMap<>();

Add value to the Map using hashmp put() method:

Here, let’s look at “How to add values ​​to hashMap using put() method”!

What is the put() method that adds a value to Map

To add values ​​to the Map, use the put method provided by the HashMap class. The hashmap put method takes the key as the first argument and the value associated with the key as the second argument.

If the Map already has a mapping for the key, the old value will be replaced with the given value.

Map<String, String> map = new HashMap<String, String>();

map .put (key name, value);

Return Value:
The hashmap put() method returns the value associated with the key in the return value.

Returns null if there is no key mapping.



adding value using hashmap put() method in java:

To add a value with Map, you need to specify the key as described above and add the value associated with the key. How to add a value by specifying a key and value in Map is described below.

package com.mycompany.javabasics;

import java.util.*;

/**

 *

 * @author Fawadkhan

 */

public class MapExamples {

public static void main(String[] args) {

// map declaration

Map<String, String>mapData = new HashMap<String, String>();

// Store data in MAP

mapData.put(“key1”, “Java”);

mapData.put(“key2”, “C++”);

mapData.put(“key3”, “Android”);

mapData.put(“key4”, “Python”);

mapData.put(“key5”, “C#”);

mapData.put(“key6”, “PHP”);

System.out.println(mapData.get(“key1”));

System.out.println(mapData.get(“key2”));

System.out.println(mapData.get(“key3”));

System.out.println(mapData.get(“key4”));

System.out.println(mapData.get(“key5”));

System.out.println(mapData.get(“key6”));

    }

}

Output:

hashmapput() method in java

In the above code, the variable mapDataof the Map interface is declared in the HashMap class, and the data type of the key and the data type of the value are each set to the String type. To add a value, use the put method to the Map variable mapDatato set the String type key name and value.



How to add values ​​to a List/Array

Like Map, List is also a dynamic array whose size is not fixed. While Map adds values ​​associated with keys, List can add elements in order like an array.

It is also possible to add a value by specifying the insertion position. In my previous article,I explained in detail how to convert map to list and list to map and I also explained howto add values ​​to the List!

Arrays have fewer procedures than Lists and Maps and can be said to be simple and easy to handle because values ​​can be set for the number of elements specified at the time of declaration.

But since the array is fixed length, not a dynamic array like Map or List, there is no way to add values. Instead, there is a method of preparing another large array for the amount you want to add and copying the values.

If you want to know how to add values ​​to an array, please refer to the article below!

How to add Multiple values to the map using putAll method in java

The HashMap class used in the Map interface has a method called putAll. The putAll method can add all values ​​in the specified Map.

The method of adding Map values ​​by specifying the putAll method is described below.

package com.mycompany.javabasics;

import java.util.*;

/**

 *

 * @author Fawadkhan

 */

public class MapExamples {

public static void main(String[] args) {

// map declaration

Map<String, String> mapData1 = new HashMap<String, String>();

Map<String, String> mapData2 = new HashMap<String, String>();

// Store data in MAP

mapData1.put(“key1”, “Java”);

mapData1.put(“key2”, “C++”);

mapData1.put(“key3”, “Python”);

mapData1.put(“key4”, “Android”);

mapData1.put(“key5”, “C#”);

mapData1.put(“key6”, “PHP”);

//add data from mapData1 to mapData2 using putAll method

mapData2.putAll(mapData1);

System.out.println(mapData2.get(“key1”));

System.out.println(mapData2.get(“key2”));

System.out.println(mapData2.get(“key3”));

System.out.println(mapData2.get(“key4”));

System.out.println(mapData2.get(“key5”));

System.out.println(mapData2.get(“key6”));

    }

}

Output:

hashmapput() method in java

In the above code, the variables mapData1 and mapData2 of the Map interface are declared, and values ​​are added to mapData1 using the hashmap put() method in java.

Next, mapData1 is specified using the putAll method on mapData2. All the values ​​of mapData1 are added to mapData2 by using the putAll method as follows.

mapData2.putAll(mapData1);



Does the method change depending on the type of Map?

Map has types such as “HashMap”, “TreeMap”, and “LinkedHashMap”.

The purpose is the same, and adding a value by specifying a key does not change, but the behavior after adding a value may change. For example, when the key names are added to the Map in the order of “key3”, “key2”, and “key1”, how each Map holds the values ​​is explained below.

HashMap

HashMap values ​​are added separately. Therefore, even if you add “key3”, “key2”, and “key1” in that order, they will be retained in random order.

TreeMap

TreeMap sorts keys in ascending order after adding values. Therefore, inside the Map, the order of “key1”, “key2”, and “key3” is maintained.

LinkedHashMap

LinkedHashMap preserves the order in which values ​​were added. Inside the Map, the order of”key3”,”key2” and”key1” is retained.




How to get the hashmap value using get() method in java

Now that you’ve added values, it’s time to know how to retrieve them.

Use the get method to get the value in Map. The get method specifies the key related to the value to get as an argument.

Map<String, String>mapData = new HashMap<String, String>();

mapData .get (key name));

Below is the program of how to get the value using the get method in java.

package com.mycompany.javabasics;

import java.util.*;

/**

 *

 * @author Fawadkhan

 */

public class MapExamples {

public static void main(String[] args) {

// map declaration

Map<String, String>mapData = new HashMap<String, String>();

// Store data in MAP

mapData.put(“key1”, “A”);

mapData.put(“key2”, “B”);

mapData.put(“key3”, “C”);

mapData.put(“key4”, “D”);

mapData.put(“key5”, “E”);

mapData.put(“key6”, “F”);

mapData.put(“key7”, “G”);

mapData.put(“key8”, “H”);

mapData.put(“key9”, “I”);

mapData.put(“key10”, “J”);

System.out.println(mapData.get(“key1”));

System.out.println(mapData.get(“key2”));

System.out.println(mapData.get(“key3”));

System.out.println(mapData.get(“key4”));

System.out.println(mapData.get(“key5”));

System.out.println(mapData.get(“key6”));

System.out.println(mapData.get(“key7”));

System.out.println(mapData.get(“key8”));

System.out.println(mapData.get(“key9”));

System.out.println(mapData.get(“key10”));

    }

}

Output:

hashmapput() method in java

If you specify the Map key in the get method like this, you can easily get the value of the Map.



Summary

I explained how to add Map values ​​using the hashmap put() method in java, how to add values ​​all at once, and the types of Map.

Collections such as Map are often used in practice, so be sure to remember how to use them. If you forgot how to add values ​​from Map, remember this article!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button