Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Hashtable keySet() Method in Java with Examples
Next article icon

Hashtable keySet() Method in Java with Examples

Last Updated : 15 Oct, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

The java.util.Hashtable is used to create a set of key elements in the hash table. It basically returns a set view of the keys, or we can create a new set and store the key elements in them.

Syntax:

public Set<K> keySet()

K : type of the Keys in the hash table

Parameters: The method does not take any parameter.

Return Value: The method returns a set having the keys of the hash table.

Below programs are used to illustrate the working of java.util.Hashtable.keySet() Method:
Example 1: 
 

Java
// Java code to illustrate the keySet() method // to get Set view of Keys from a Hashtable.  import java.util.Enumeration; import java.util.Iterator; import java.util.Hashtable; import java.util.Set;  public class Example1 {      public static void main(String[] args)     {          // Creating an empty Hashtable         Hashtable<String, String> hash_t             = new Hashtable<String, String>();          // Add mappings into the table         hash_t.put("1", "Geeks");         hash_t.put("2", "For");         hash_t.put("3", "Geeks");          // Getting a Set of keys using         // keySet() method of Hashtable class         Set hash_set = hash_t.keySet();          System.out.println(             "Set created from Hashtable Keys contains :");          // Iterating through the Set of keys         Iterator itr = hash_set.iterator();         while (itr.hasNext())             System.out.println(itr.next());     } } 

Output
Set created from Hashtable Keys contains :  3  2  1


Example 2:

Java
// Java code to illustrate the keySet() method // to get Set view of Keys from a Hashtable.  import java.util.Enumeration; import java.util.Iterator; import java.util.Hashtable; import java.util.Set;  public class Example2 {      public static void main(String[] args)     {          // Creating an empty Hashtable         Hashtable<String, String> hash_t             = new Hashtable<String, String>();          // Inserting elements into the table         hash_t.put("Geeks", "1");         hash_t.put("For", "2");         hash_t.put("geeks", "3");          // Getting a Set of keys using         // keySet() method of Hashtable class         Set hash_set = hash_t.keySet();          System.out.println(             "Set created from Hashtable Keys contains :");          // Iterating through the Set of keys         Iterator itr = hash_set.iterator();         while (itr.hasNext())             System.out.println(itr.next());     } } 

Output
Set created from Hashtable Keys contains :  For  Geeks  geeks


 Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.


Next Article
Hashtable keySet() Method in Java with Examples

S

sayaliparulekar
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java-HashTable
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    Map keySet() Method in Java with Examples
    This method is used to return a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. Example:Java// Java Program Implementing Map // keySet() Method import java.util.*; public class GfG { public static void main(Str
    2 min read
    KeyStore getKey() method in Java with Examples
    The getKey() method of java.security.KeyStore class is used to get the key associated with the given alias, using the given password to recover it. Syntax: public final Key getKey(String alias, char[] password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException Parameter:
    3 min read
    LinkedHashSet hashCode() method in Java with Example
    The hashCode() method of LinkedHashSet in Java is used to get the hashCode value for this instance of the LinkedHashSet. It returns an integer value which is the hashCode value for this instance of the LinkedHashSet. Syntax: public int hashCode() Parameters: This function has no parameters. Returns:
    2 min read
    TreeMap keySet() Method in Java with Examples
    In Java, keySet() method of TreeMap class is present inside java.util package in Java is used to create a set out of the key elements contained in the treemap. It basically returns a set view of the keys or we can create a new set and store the key elements in them in ascending order. Since the set
    3 min read
    HashTable putIfAbsent() method in Java with Examples
    The putIfAbsent(Key, value) method of Hashtable class which allows to map a value to a given key if given key is not associated with a value or mapped to null. A null value is returned if such key-value set is already present in the HashMap. Syntax: public V putIfAbsent(K key, V value) Parameters: T
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences