Retrieve Hashtable elements

We know that HashTable is a key-value pair based data structure.

Sometimes, we need to retrieve all the keys of HashTable

Sometimes we need to retrieve value for the specified key from the HashTable

Sometimes we need to retrieve all the values from a HashTable

Let’s see how we can achieve all these retrievals

Case1:

Retrieve all the keys from HashTable

We can use keyset() method of HashTable to get all the keys in that Map.

Example

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

Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
 
      hashTable.put(1, "One");
      hashTable.put(2, "Two");
      hashTable.put(3, "Three");
Set<Integer> keys = hashTable.keySet();
System.out.println("All the keys of HashTable....");
System.out.println(keys);

}
}


Case2 :

Retrieve all the values from HashTable

We can use values() method to retrieve all the values from the HashTable

Example

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

Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
      hashTable.put(1, "One");
      hashTable.put(2, "Two");
      hashTable.put(3, "Three");
Collection<String> values = hashTable.values();
System.out.println("All the values of HashTable....");
System.out.println(values);

}
}


Case3 :

Retrieve value for the specified key from HashTable

We can also retrieve value for a specific key from the HashTable using get() method by passing a specific key.

Example

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

Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
      hashTable.put(1, "One");
      hashTable.put(2, "Two");
      hashTable.put(3, "Three");
String value = hashTable.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