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
- 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);
- }
- }
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
- 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);
- }
- }
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
- 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);
- }
- }
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); } }