Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
java

What is the HashTable.get() method in Java?

Sumit Sharma

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The HashTable.get() method is present in the HashTable class inside the java.util package.

The HashTable.get() method is used to retrieve the value of the passed key from the HashTable.

Parameter

The HashTable.get() method takes one parameter:

  • key: The key whose corresponding value needs to be retrieved from the HashTable.

Return

The HashTable.get() method returns the value elements associated with the key.

Code

Let’s take a look at the below code snippet.

import java.util.*;
class Main
{
public static void main(String[] args)
{
Hashtable<Integer, String> h1 = new Hashtable<Integer, String>();
h1.put(1, "Let's");
h1.put(5, "see");
h1.put(2, "Hashtable.contains()");
h1.put(27, "method");
h1.put(9, "in java.");
System.out.println("The value with key=9 is: " + h1.get(9));
System.out.println("The value with key=5 is: " + h1.get(5));
System.out.println("The value with key=27 is: " + h1.get(27));
}
}
Using the HashTable.get() method in Java

Explanation

  • In line 1, we import the required package.
  • In line 2, we make a Main class.
  • In line 4, we make a main function.
  • In line 6, we declare a HashTable consisting of Integer type keys and String type values.
  • In lines 8 to 12, we insert values in the HashTable by using the Hashtable.put() method.
  • In lines 14 to 16, we display the value element for the corresponding key passed as an argument by using the HashTable.get() method.

In this way, we can use HashTable.get() method to retrieve the value of the passed key from the HashTable.

RELATED TAGS

communitycreator
java

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring