HashMap get() Method: Get Value By Key in Java using NetBeans
Description:
In this article, you will learn the hashmap get() method. I will use this method in java programming practically using NetBeans IDE.
What is Map (associative array)?
A Map is a collection that holds key-value pairs. A collection is like a dynamic array that can be resized later. You can specify a key when adding a value, and use the key to look up and retrieve the value.
Duplicate keys are not allowed and keys and values ​​can only have reference types. Maps are also called associative arrays.
get value by key of the HashMap using get() method:
To get the value of Map, use the Hashmap get() method and specify the key of Map to get the value associated with the key. How to specify the key in Map and get the value by key 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”, “Fawad khan”);
mapData.put(“key2”, “EngrFahad”);
mapData.put(“key3”, “hamza Khan”);
mapData.put(“key4”, “Faraz”);
mapData.put(“key5”, “Ghazanfar javid”);
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”));
}
}
Output:
In the above code, a Map type variable mapData is declared, and the key and data to be set are declared in String type. We use the put method to store the key name and its associated value.
After storing a value using the put method, you can retrieve the stored value by specifying the key using the Hashmap get() method.
How to get all keys of the HashMap:
To get all the keys of Map, use the Hashmap keySet() method. The keySet() method returns the Map key as a Set type, so it can be obtained repeatedly using the enhanced for loop(for-each) as follows.
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”, “Fawad khan”);
mapData.put(“key2”, “Engr Fahad”);
mapData.put(“key3”, “hamza Khan”);
mapData.put(“key4”, “Faraz”);
mapData.put(“key5”, “Ghazanfar javid”);
for (String key : mapData.keySet()) {
System.out.println(key);
}
}
}
Output:
How to get all values of the HashMap:
To get all the values ​​of the Map, use the hashmap values() ​​method. The values ​​method also returns the values ​​of the Map as a Set type, so you can use the enhanced for loop(for-each) to repeatedly retrieve them as follows.
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”, “Fawad khan”);
mapData.put(“key2”, “Engr Fahad”);
mapData.put(“key3”, “hamza Khan”);
mapData.put(“key4”, “Faraz”);
mapData.put(“key5”, “Ghazanfar javid”);
for (String ValueData : mapData.values()) {
System.out.println(ValueData);
}
}
}
Output:
How to get all key-value pairs (Entry)
Map key-value pairs are represented by the Map.Entry interface. Map.Entry can get the key with the hashmap getKey() method and the value with the getValue method. To get all entries of Map, use entrySet method.
Since the entrySet method returns the Entry of the Map as a Set type, it can be obtained repeatedly using the enhanced for loop(for-each) as follows.
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”, “Fawad khan”);
mapData.put(“key2”, “EngrFahad”);
mapData.put(“key3”, “hamza Khan”);
mapData.put(“key4”, “Faraz”);
mapData.put(“key5”, “Ghazanfarjavid”);
for (Map.Entry<String, String> entry : mapData.entrySet()) {
System.out.println(entry.getKey() + “:” + entry.getValue());
}
}
}
Output:
convert Map to List and get value by element number
Here, as an applied usage of Map, I will explain how to get the value by the element number. As explained in Map, it is common to get the value associated with the specified key.
However, if you convert Map to List of the same collection, you can specify the element number and get the value. To convert a Map to a List, use the values ​​method:
How to write:
List <value data type> object name = new ArrayList<value data type>( Map type object name. values ​​());  Â
The values ​​method returns all values ​​stored in the Map. The method to convert the value stored in Map to List and get the value by element number is described below.
How to convert Map to List and get value by element number:
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”, “Fawad khan”);
mapData.put(“key2”, “Engr Fahad”);
mapData.put(“key3”, “hamza Khan”);
mapData.put(“key4”, “Faraz”);
mapData.put(“key5”, “Ghazanfar javid”);
// Declare a List and use the values method to get the Map values
List<String>listData = new ArrayList<>(mapData.values());
// Get the List value by specifying the element number
System.out.println(listData.get(0));
System.out.println(listData.get(1));
System.out.println(listData.get(2));
System.out.println(listData.get(3));
System.out.println(listData.get(4));
   }
}
Output:
In the above code, a Map type variable mapData is declared as before, and the key and value to be stored are specified in String type. i use the hashmap put() method to store the key name and the data associated with it.
Next, we declare a List type variable listData of the Array List class and use the values ​​method to convert the Map to a List. It is possible to specify the number of elements in the get method of List and get the value.
Summary
How was it? I found out how to get the key and value of Map using Hashmap get() method, and how to convert Map to List and get the value by specifying the element number. Lists and maps can be even more useful if you use them in advanced ways.
If you forgot how to get values ​​from Map, remember this article!