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

  1. import java.util.*;
  2. public class HashMapRetrieveKeysExample {
  3. public static void main(String args[]) {
  4.  
  5. HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
  6.       hashMap.put(1, "One");
  7.       hashMap.put(2, "Two");
  8.       hashMap.put(3, "Three");
  9. Set<Integer> keys = hashMap.keySet();
  10. System.out.println("All the keys of HashMap....");
  11. System.out.println(keys);
  12.  
  13. }
  14. }
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

  1. import java.util.*;
  2. public class HashMapRetrieveAllValuesExample {
  3. public static void main(String args[]) {
  4.  
  5. HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
  6.       hashMap.put(1, "One");
  7.       hashMap.put(2, "Two");
  8.       hashMap.put(3, "Three");
  9. Collection<String> values = hashMap.values();
  10. System.out.println("All the values of HashMap....");
  11. System.out.println(values);
  12.  
  13. }
  14. }
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

  1. import java.util.*;
  2. public class HashMapRetrieveValueForKeyExample {
  3. public static void main(String args[]) {
  4.  
  5. HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
  6.       hashMap.put(1, "One");
  7.       hashMap.put(2, "Two");
  8.       hashMap.put(3, "Three");
  9. String value = hashMap.get(1);
  10. System.out.println("Value for key 1....");
  11. System.out.println(value);
  12.  
  13. }
  14. }
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);

}
}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on