Retrieve HashMap elements
We know that HashMap is a key-value pair based data structure.
Sometimes, we need to retrieve all the keys of HashMap
Sometimes we need to retrieve value for the specified key from the HashMap
Sometimes we need to retrieve all the values from a HashMap
Let’s see how we can achieve all these retrievals
Case1:
Retrieve all the keys from HashMap
We can use keyset() method of HashMap to get all the keys in that Map.
Example
- import java.util.*;
- public class HashMapRetrieveKeysExample {
- public static void main(String args[]) {
- HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
- hashMap.put(1, "One");
- hashMap.put(2, "Two");
- hashMap.put(3, "Three");
- Set<Integer> keys = hashMap.keySet();
- System.out.println("All the keys of HashMap....");
- System.out.println(keys);
- }
- }
import java.util.*; public class HashMapRetrieveKeysExample { public static void main(String args[]) { HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(1, "One"); hashMap.put(2, "Two"); hashMap.put(3, "Three"); Set<Integer> keys = hashMap.keySet(); System.out.println("All the keys of HashMap...."); System.out.println(keys); } }
Case2 :
Retrieve all the values from HashMap
We can use values() method to retrieve all the values from the HashMap
Example
- import java.util.*;
- public class HashMapRetrieveAllValuesExample {
- public static void main(String args[]) {
- HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
- hashMap.put(1, "One");
- hashMap.put(2, "Two");
- hashMap.put(3, "Three");
- Collection<String> values = hashMap.values();
- System.out.println("All the values of HashMap....");
- System.out.println(values);
- }
- }
import java.util.*; public class HashMapRetrieveAllValuesExample { public static void main(String args[]) { HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(1, "One"); hashMap.put(2, "Two"); hashMap.put(3, "Three"); Collection<String> values = hashMap.values(); System.out.println("All the values of HashMap...."); System.out.println(values); } }
Case3 :
Retrieve value for the specified key from HashMap
We can also retrieve value for a specific key from the HashMap using get() method by passing a specific key.
Example
- import java.util.*;
- public class HashMapRetrieveValueForKeyExample {
- public static void main(String args[]) {
- HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
- hashMap.put(1, "One");
- hashMap.put(2, "Two");
- hashMap.put(3, "Three");
- String value = hashMap.get(1);
- System.out.println("Value for key 1....");
- System.out.println(value);
- }
- }
import java.util.*; public class HashMapRetrieveValueForKeyExample { public static void main(String args[]) { HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(1, "One"); hashMap.put(2, "Two"); hashMap.put(3, "Three"); String value = hashMap.get(1); System.out.println("Value for key 1...."); System.out.println(value); } }