- Blog Categories
- Software Development
- Data Science
- AI/ML
- Marketing
- General
- MBA
- Management
- Legal
- Software Development Projects and Ideas
- 12 Computer Science Project Ideas
- 28 Beginner Software Projects
- Top 10 Engineering Project Ideas
- Top 10 Easy Final Year Projects
- Top 10 Mini Projects for Engineers
- 25 Best Django Project Ideas
- Top 20 MERN Stack Project Ideas
- Top 12 Real Time Projects
- Top 6 Major CSE Projects
- 12 Robotics Projects for All Levels
- Java Programming Concepts
- Abstract Class in Java and Methods
- Constructor Overloading in Java
- StringBuffer vs StringBuilder
- Java Identifiers: Syntax & Examples
- Types of Variables in Java Explained
- Composition in Java: Examples
- Append in Java: Implementation
- Loose Coupling vs Tight Coupling
- Integrity Constraints in DBMS
- Different Types of Operators Explained
- Career and Interview Preparation in IT
- Top 14 IT Courses for Jobs
- Top 20 Highest Paying Languages
- 23 Top CS Interview Q&A
- Best IT Jobs without Coding
- Software Engineer Salary in India
- 44 Agile Methodology Interview Q&A
- 10 Software Engineering Challenges
- Top 15 Tech's Daily Life Impact
- 10 Best Backends for React
- Cloud Computing Reference Models
- Web Development and Security
- Find Installed NPM Version
- Install Specific NPM Package Version
- Make API Calls in Angular
- Install Bootstrap in Angular
- Use Axios in React: Guide
- StrictMode in React: Usage
- 75 Cyber Security Research Topics
- Top 7 Languages for Ethical Hacking
- Top 20 Docker Commands
- Advantages of OOP
- Data Science Projects and Applications
- 42 Python Project Ideas for Beginners
- 13 Data Science Project Ideas
- 13 Data Structure Project Ideas
- 12 Real-World Python Applications
- Python Banking Project
- Data Science Course Eligibility
- Association Rule Mining Overview
- Cluster Analysis in Data Mining
- Classification in Data Mining
- KDD Process in Data Mining
- Data Structures and Algorithms
- Binary Tree Types Explained
- Binary Search Algorithm
- Sorting in Data Structure
- Binary Tree in Data Structure
- Binary Tree vs Binary Search Tree
- Recursion in Data Structure
- Data Structure Search Methods: Explained
- Binary Tree Interview Q&A
- Linear vs Binary Search
- Priority Queue Overview
- Python Programming and Tools
- Top 30 Python Pattern Programs
- List vs Tuple
- Python Free Online Course
- Method Overriding in Python
- Top 21 Python Developer Skills
- Reverse a Number in Python
- Switch Case Functions in Python
- Info Retrieval System Overview
- Reverse a Number in Python
- Real-World Python Applications
- Data Science Careers and Comparisons
- Data Analyst Salary in India
- Data Scientist Salary in India
- Free Excel Certification Course
- Actuary Salary in India
- Data Analyst Interview Guide
- Pandas Interview Guide
- Tableau Filters Explained
- Data Mining Techniques Overview
- Data Analytics Lifecycle Phases
- Data Science Vs Analytics Comparison
- Artificial Intelligence and Machine Learning Projects
- Exciting IoT Project Ideas
- 16 Exciting AI Project Ideas
- 45+ Interesting ML Project Ideas
- Exciting Deep Learning Projects
- 12 Intriguing Linear Regression Projects
- 13 Neural Network Projects
- 5 Exciting Image Processing Projects
- Top 8 Thrilling AWS Projects
- 12 Engaging AI Projects in Python
- NLP Projects for Beginners
- Concepts and Algorithms in AIML
- Basic CNN Architecture Explained
- 6 Types of Regression Models
- Data Preprocessing Steps
- Bagging vs Boosting in ML
- Multinomial Naive Bayes Overview
- Bayesian Network Example
- Bayes Theorem Guide
- Top 10 Dimensionality Reduction Techniques
- Neural Network Step-by-Step Guide
- Technical Guides and Comparisons
- Make a Chatbot in Python
- Compute Square Roots in Python
- Permutation vs Combination
- Image Segmentation Techniques
- Generative AI vs Traditional AI
- AI vs Human Intelligence
- Random Forest vs Decision Tree
- Neural Network Overview
- Perceptron Learning Algorithm
- Selection Sort Algorithm
- Career and Practical Applications in AIML
- AI Salary in India Overview
- Biological Neural Network Basics
- Top 10 AI Challenges
- Production System in AI
- Top 8 Raspberry Pi Alternatives
- Top 8 Open Source Projects
- 14 Raspberry Pi Project Ideas
- 15 MATLAB Project Ideas
- Top 10 Python NLP Libraries
- Naive Bayes Explained
- Digital Marketing Projects and Strategies
- 10 Best Digital Marketing Projects
- 17 Fun Social Media Projects
- Top 6 SEO Project Ideas
- Digital Marketing Case Studies
- Coca-Cola Marketing Strategy
- Nestle Marketing Strategy Analysis
- Zomato Marketing Strategy
- Monetize Instagram Guide
- Become a Successful Instagram Influencer
- 8 Best Lead Generation Techniques
- Digital Marketing Careers and Salaries
- Digital Marketing Salary in India
- Top 10 Highest Paying Marketing Jobs
- Highest Paying Digital Marketing Jobs
- SEO Salary in India
- Content Writer Salary Guide
- Digital Marketing Executive Roles
- Career in Digital Marketing Guide
- Future of Digital Marketing
- MBA in Digital Marketing Overview
- Digital Marketing Techniques and Channels
- 9 Types of Digital Marketing Channels
- Top 10 Benefits of Marketing Branding
- 100 Best YouTube Channel Ideas
- YouTube Earnings in India
- 7 Reasons to Study Digital Marketing
- Top 10 Digital Marketing Objectives
- 10 Best Digital Marketing Blogs
- Top 5 Industries Using Digital Marketing
- Growth of Digital Marketing in India
- Top Career Options in Marketing
- Interview Preparation and Skills
- 73 Google Analytics Interview Q&A
- 56 Social Media Marketing Q&A
- 78 Google AdWords Interview Q&A
- Top 133 SEO Interview Q&A
- 27+ Digital Marketing Q&A
- Digital Marketing Free Course
- Top 9 Skills for PPC Analysts
- Movies with Successful Social Media Campaigns
- Marketing Communication Steps
- Top 10 Reasons to Be an Affiliate Marketer
- Career Options and Paths
- Top 25 Highest Paying Jobs India
- Top 25 Highest Paying Jobs World
- Top 10 Highest Paid Commerce Job
- Career Options After 12th Arts
- Top 7 Commerce Courses Without Maths
- Top 7 Career Options After PCB
- Best Career Options for Commerce
- Career Options After 12th CS
- Top 10 Career Options After 10th
- 8 Best Career Options After BA
- Projects and Academic Pursuits
- 17 Exciting Final Year Projects
- Top 12 Commerce Project Topics
- Top 13 BCA Project Ideas
- Career Options After 12th Science
- Top 15 CS Jobs in India
- 12 Best Career Options After M.Com
- 9 Best Career Options After B.Sc
- 7 Best Career Options After BCA
- 22 Best Career Options After MCA
- 16 Top Career Options After CE
- Courses and Certifications
- 10 Best Job-Oriented Courses
- Best Online Computer Courses
- Top 15 Trending Online Courses
- Top 19 High Salary Certificate Courses
- 21 Best Programming Courses for Jobs
- What is SGPA? Convert to CGPA
- GPA to Percentage Calculator
- Highest Salary Engineering Stream
- 15 Top Career Options After Engineering
- 6 Top Career Options After BBA
- Job Market and Interview Preparation
- Why Should You Be Hired: 5 Answers
- Top 10 Future Career Options
- Top 15 Highest Paid IT Jobs India
- 5 Common Guesstimate Interview Q&A
- Average CEO Salary: Top Paid CEOs
- Career Options in Political Science
- Top 15 Highest Paying Non-IT Jobs
- Cover Letter Examples for Jobs
- Top 5 Highest Paying Freelance Jobs
- Top 10 Highest Paying Companies India
- Career Options and Paths After MBA
- 20 Best Careers After B.Com
- Career Options After MBA Marketing
- Top 14 Careers After MBA In HR
- Top 10 Highest Paying HR Jobs India
- How to Become an Investment Banker
- Career Options After MBA - High Paying
- Scope of MBA in Operations Management
- Best MBA for Working Professionals India
- MBA After BA - Is It Right For You?
- Best Online MBA Courses India
- MBA Project Ideas and Topics
- 11 Exciting MBA HR Project Ideas
- Top 15 MBA Project Ideas
- 18 Exciting MBA Marketing Projects
- MBA Project Ideas: Consumer Behavior
- What is Brand Management?
- What is Holistic Marketing?
- What is Green Marketing?
- Intro to Organizational Behavior Model
- Tech Skills Every MBA Should Learn
- Most Demanding Short Term Courses MBA
- MBA Salary, Resume, and Skills
- MBA Salary in India
- HR Salary in India
- Investment Banker Salary India
- MBA Resume Samples
- Sample SOP for MBA
- Sample SOP for Internship
- 7 Ways MBA Helps Your Career
- Must-have Skills in Sales Career
- 8 Skills MBA Helps You Improve
- Top 20+ SAP FICO Interview Q&A
- MBA Specializations and Comparative Guides
- Why MBA After B.Tech? 5 Reasons
- How to Answer 'Why MBA After Engineering?'
- Why MBA in Finance
- MBA After BSc: 10 Reasons
- Which MBA Specialization to choose?
- Top 10 MBA Specializations
- MBA vs Masters: Which to Choose?
- Benefits of MBA After CA
- 5 Steps to Management Consultant
- 37 Must-Read HR Interview Q&A
- Fundamentals and Theories of Management
- What is Management? Objectives & Functions
- Nature and Scope of Management
- Decision Making in Management
- Management Process: Definition & Functions
- Importance of Management
- What are Motivation Theories?
- Tools of Financial Statement Analysis
- Negotiation Skills: Definition & Benefits
- Career Development in HRM
- Top 20 Must-Have HRM Policies
- Project and Supply Chain Management
- Top 20 Project Management Case Studies
- 10 Innovative Supply Chain Projects
- Latest Management Project Topics
- 10 Project Management Project Ideas
- 6 Types of Supply Chain Models
- Top 10 Advantages of SCM
- Top 10 Supply Chain Books
- What is Project Description?
- Top 10 Project Management Companies
- Best Project Management Courses Online
- Salaries and Career Paths in Management
- Project Manager Salary in India
- Average Product Manager Salary India
- Supply Chain Management Salary India
- Salary After BBA in India
- PGDM Salary in India
- Top 7 Career Options in Management
- CSPO Certification Cost
- Why Choose Product Management?
- Product Management in Pharma
- Product Design in Operations Management
- Industry-Specific Management and Case Studies
- Amazon Business Case Study
- Service Delivery Manager Job
- Product Management Examples
- Product Management in Automobiles
- Product Management in Banking
- Sample SOP for Business Management
- Video Game Design Components
- Top 5 Business Courses India
- Free Management Online Course
- SCM Interview Q&A
- Fundamentals and Types of Law
- Acceptance in Contract Law
- Offer in Contract Law
- 9 Types of Evidence
- Types of Law in India
- Introduction to Contract Law
- Negotiable Instrument Act
- Corporate Tax Basics
- Intellectual Property Law
- Workmen Compensation Explained
- Lawyer vs Advocate Difference
- Law Education and Courses
- LLM Subjects & Syllabus
- Corporate Law Subjects
- LLM Course Duration
- Top 10 Online LLM Courses
- Online LLM Degree
- Step-by-Step Guide to Studying Law
- Top 5 Law Books to Read
- Why Legal Studies?
- Pursuing a Career in Law
- How to Become Lawyer in India
- Career Options and Salaries in Law
- Career Options in Law India
- Corporate Lawyer Salary India
- How To Become a Corporate Lawyer
- Career in Law: Starting, Salary
- Career Opportunities: Corporate Law
- Business Lawyer: Role & Salary Info
- Average Lawyer Salary India
- Top Career Options for Lawyers
- Types of Lawyers in India
- Steps to Become SC Lawyer in India
- Tutorials
- Software Tutorials
- C Tutorials
- Recursion in C: Fibonacci Series
- Checking String Palindromes in C
- Prime Number Program in C
- Implementing Square Root in C
- Matrix Multiplication in C
- Understanding Double Data Type
- Factorial of a Number in C
- Structure of a C Program
- Building a Calculator Program in C
- Compiling C Programs on Linux
- Java Tutorials
- Handling String Input in Java
- Determining Even and Odd Numbers
- Prime Number Checker
- Sorting a String
- User-Defined Exceptions
- Understanding the Thread Life Cycle
- Swapping Two Numbers
- Using Final Classes
- Area of a Triangle
- Skills
- Explore Skills
- Management Skills
- Software Engineering
- JavaScript
- Data Structure
- React.js
- Core Java
- Node.js
- Blockchain
- SQL
- Full stack development
- Devops
- NFT
- BigData
- Cyber Security
- Cloud Computing
- Database Design with MySQL
- Cryptocurrency
- Python
- Digital Marketings
- Advertising
- Influencer Marketing
- Performance Marketing
- Search Engine Marketing
- Email Marketing
- Content Marketing
- Social Media Marketing
- Display Advertising
- Marketing Analytics
- Web Analytics
- Affiliate Marketing
- MBA
- MBA in Finance
- MBA in HR
- MBA in Marketing
- MBA in Business Analytics
- MBA in Operations Management
- MBA in International Business
- MBA in Information Technology
- MBA in Healthcare Management
- MBA In General Management
- MBA in Agriculture
- MBA in Supply Chain Management
- MBA in Entrepreneurship
- MBA in Project Management
- Management Program
- Consumer Behaviour
- Supply Chain Management
- Financial Analytics
- Introduction to Fintech
- Introduction to HR Analytics
- Fundamentals of Communication
- Art of Effective Communication
- Introduction to Research Methodology
- Mastering Sales Technique
- Business Communication
- Fundamentals of Journalism
- Economics Masterclass
- Free Courses
What is Hashtable in Java? Explained with Examples
Updated on 03 July, 2023
7.51K+ views
• 17 min read
Table of Contents
What is Hashtable in Java?
Hashtable in java maps the keys to values. The Hashtable inherits the Dictionary class and implements the map interface. Any non-null object can be used as a value or a key. The objects used as keys in Hashtable must implement the “hashcode” method and the “equals” method. It was a part of “original java.util” but got the power to implement the map interface in Java 2.
Hashtable is integrated into collections framework in java and is similar to HashMap except that Hashtable is synchronized.
Hashtable in Java Features
A hashtable in Java is a part of the Java collections framework and implements the Map interface. It is backwards compatible because it derives from the completely worthless and dated Dictionary class. After understanding hashmap and hashtable in Java, let’s look at some of its salient characteristics:
- Key-Based Hashtables: As opposed to arrays, a hashtable in Java only requires a key to find the value, which could be a different object.
- Dynamic Capacity: The hashmap and hashtable in Java are capable of holding greater numbers of elements than the internal array can manage by using chaining and linked lists.
- Performance: The Java hashtables performance may be O(n) in the worst-case scenario when you must traverse a linked list to find the correct value object due to collision, irrespective of knowing that hashtables are made for quick searches or O(1) searches (constant time search operations).
Note: Since worst-case performance is now capped at O(logN), and JDK uses binary trees as opposed to linked lists from Java 8, performance has been significantly enhanced.
- Application: Maps or pairs of key-value items are stored in hashtables. As a result, it might be used to store client information, library books, or other documents.
- Sorting: A hashtable can’t be sorted. However, data that is sorted can be obtained and extracted by classifying the hash table’s list of keys and then extracting values via a LinkedHashMap or TreeMap to retrieve the values in the proper order.
- Collision: Collisions can happen in hashtables because distinct data items may collide and be related to the same bucket. That’s because their hash values clash. Different collision avoidance strategies, such as open addressing or chaining, can be used to tackle this.
- Synchronization: The hashtable class is thread-safe because it is synchronized. More than one thread can’t get to the Hashtable class instance at once. Because of this, its operations are slower than Java’s HashMaps.
Note: Using HashMap rather than Hashtable in Java is advised if a thread-safe implementation isn’t necessary.
Java Hashtable Example
import java.util.*;
class Hashtable1{
public static void main(String args[]){
Hashtable<Integer,String> hm=new Hashtable<Integer,String>();
hm.put(200,"Rohan");
hm.put(202,"Rohit");
hm.put(201,"Raunak");
hm.put(203,"Ramesh");
for(Map.Entry m:hm.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
Output:
203 Ramesh
202 Raunak
201 Rohit
200 Rohan
Here is another Java hashtable example for the remove() operation:
import java.util.*;
public class Hashtable2 {
public static void main(String args[]) {
Hashtable<Integer,String> map=new Hashtable<Integer,String>();
map.put(200,"Rohan");
map.put(202,"Raunak");
map.put(201,"Rohit");
map.put(203,"Ramesh");
System.out.println("Before remove: "+ map);
// Remove value for key 202
map.remove(202);
System.out.println("After remove: "+ map);
}
}
Output:
Before remove: {203=Ramesh, 202=Raunak, 201=Rohit, 200=Rohan}
After remove: {203=Ramesh, 201=Rohit, 200=Rohan}
With these two Java hashtable examples as well as the understanding of hashtable’s salient features, you should now have a solid grasp of this Java concept.
Check out our free technology courses to get an edge over the competition.
Hierarchy of Hashtable in Java
The direct subclasses of Hashtable in java are Properties and UIDefaults.
- Hashtable in java implements Serializable and Cloneable.
- Hashtable in java extends Dictionary<K,V>.
Explore our Popular Software Engineering Courses
Class Declaration- Hashtable in Java
The declaration for java.util.Hashtable class is:
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable
Class Parameters- Hashtable in Java
The parameters for java.util.Hashtable class are:
- K: It stands for the different key types maintained by this map.
- V: It stands for mapped value types.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
Working of Hashtable in Java
Hashtable is a data structure that stores keys or value pairs and is an array of buckets. The determination of the mapping of a bucket of keys or value pairs is achieved using the hashCode() method.
This hashcode is a non-negative integer that is equal for equal objects. This may or may not be equal for unequal objects. The Hashtable in Java uses the equals() method to check whether two objects are equal or not.
Thus, the hash function determines the location for a given key in the bucket list. In simple words, an object is used as a key, and value is linked to the key. The hashing of key results in a hashcode. This hashcode represents the index at which values are stored in the Hashtable.
What is Collision in Hashtable in Java?
When two unequal objects have the same hashcodes in Hashtable, the condition is termed as collision. An array of lists is used to resolve collision in Hashtable. It is resolved by storing the pairs mapped to a single bucket in a list. This list reference is then stored in the array index.
In case of hash collision, the multiple entries in a single bucket must be searched sequentially.
What are the Parameters affecting the Performance of Hashtable in Java?
The main parameters that affect the performance of any instance of Hashtable are:
- Initial Capacity:
The number of buckets in a Hashtable is called its capacity. The capacity of the Hashtable at the time of its creation is its initial capacity. It controls a tradeoff between wasted space and rehashes operations’ requirements. Thus setting high values of initial capacity wastes space in Hashtable. If the initial capacity is greater than the maximum number of entries, no rehash operations are possible.
- Load Factor:
The measure of how full the Hashtable is allowed before its capacity is automatically increased is termed as the load factor of Hashtable in java. The default load factor value is 0.75, and it allows a good tradeoff between time and space costs. With an increase in load factor, the time cost to lookup an entry is increased, and space overhead decreases.
Constructors- Hashtable in Java
We need to import Hashtable in java from java.util.Hashtable. The different ways in which Hashtable can be created in java are:
Hashtable():
It creates an empty Hashtable with an initial capacity of 11 and the default load factor of 0.75. It can be explained using the following example.
Hashtable<K,V> ht = new Hashtable<K,V> ();
Input:
Output:
Hashtable(int initialCapacity):
It creates a Hashtable with a default load factor of 0.75. The initial capacity is defined by the initialCapacity. It can be explained using the following example.
Hashtable<K,V> ht = new Hashtable<K,V> (int initialCapacity);
Input:
Output:
Hashtable(int size, float fillRatio):
It creates a Hashtable whose fill ratio is defined by fillRatio. It defines that how full a Hashtable can be before it is resized upwardly. The initial size in this constructor is specified by “size.” It can be explained using the following example.
Hashtable<K,V> ht = new Hashtable<K,V> (int size, float fillRatio);
Input:
Output:
Hashtable(Map<? Extends K,? extends V>m)
It creates a Hashtable that is initialized with the elements of “m.” It can be explained using the following example.
Hashtable<K,V> ht = new Hashtable<K,V> (Map m);
Input:
Output:
Different Methods of Hashtable in Java
The different methods in the Java Hashtable class and their description are as below:
void clear() Method:
This method is used to reset the Hashtable.
Enumeration elements() Method:
An enumeration of the values in the Hashtable is returned by this method.
Object clone() Method:
This method returns a shallow copy of the Hashtable.
Set<Map.Entry<K,V>> entrySet() Method:
To return a set view of the mappings contained in the map, this method is used.
boolean equals(Object o) Method:
This method is used to compare the specified object within the hashmap of the table.
int hashCode() Method:
This method returns the hash code value for the map.
Set<K> keySet() Method:
This method returns a Set view of the keys present in the map.
V put(K key, V value) Method:
This method inserts the defined value with the defined key in the Hashtable.
boolean remove(Object key, Object value) Method:
This method removes the specified values with the associated specified keys from the Hashtable.
V replace(K key, V value) Method:
This method replaces the defined value for a defined key.
String toString()Method:
This method returns the representation of the objects in Hashtable.
Collection values() Method:
This method returns a collection view of the values present in the map.
boolean contains(Object value) Method:
It returns to true if there is some value equal to the value that exists from the Hashtable. It otherwise returns false.
boolean containsValue(Object value) Method:
It returns true if there is some value equal to the value that exists from the Hashtable. It otherwise returns false.
boolean containsKey(Object key) Method:
It returns true if some key equal to the key exists in the Hashtable. It otherwise returns false.
boolean isEmpty() Method:
It returns true if the Hashtable is empty. It otherwise returns to false if it contains at least one key.
protected void rehash() Method:
This method is used to increase the size of the Hashtable and rehashes all of its keys.
V get(Object key) Method:
It returns the object that contains the value attached with the key.
V remove(Object key) Method:
This method is used to remove the key and its value. This method returns the value defined with the key.
In-Demand Software Development Skills
int size() Method:
It returns to the number of entries present in the Hashtable.
V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) Method:
This method computes the mapping for any specified key. It also computes its current mapped value and goes to null if there is no current mapping.
V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) Method:
This method computes its value using the given mapping function if the specified key is not already associated with a value or to the condition in which it is mapped to null). It also enters it into this map unless it achieves null.
V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) Method:
It further computes its current mapped value if the value for the specified key is present and is non-empty, along with its new mapping.
void forEach(BiConsumer<? super K,? super V> action) Method:
This method performs the given action for each entry in the map until all entries are done.
V getOrDefault(Object key, V defaultValue) Method:
This method returns to the value to which the defined key is mapped. It also returns to defaultValue if the map contains no mapping for the key.
V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) Method:
This method associates a defined key with the given non-null value if it is not already associated with a value or is associated with null.
boolean replace(K key, V oldValue, V newValue):
This method replaces the old value with the new value for the specified key.
void putAll(Map<? extends K,? extends V> t)) Method:
This method copies all the key-value pair from the map to the Hashtable.
V putIfAbsent(K key, V value) Method:
This method associates the specified key with the given value. It returns null if it is already associated with a value (or is mapped to null), else it returns the current value.
void replaceAll(BiFunction<? super K,? super V,? extends V> function) Method:
It replaces the entry’s value with the result of introducing the given function on that entry only. It is done until all entries processor until the condition in which the function throws an exception.
Different Operations on Hashtable in Java
The different operations on Hashtable can be performed in the following ways:
Adding Elements:
The put() method is used to add an element to the Hashtable. Internally, a separate hash is generated for each element. The elements are indexed based on this hash only to make it more efficient. The insertion order is not retained in the Hashtable. For example:
Input:
Output:
Changing Elements:
If you want to change an element after adding, it can be done by again the element by using put(). The elements in a Hashtable are indexed using the keys. Thus, the key’s value can be changed by inserting the updated value of the key. For example:
Input:
Output:
Removing Elements:
The remove() method is used to remove an element from the map. It takes the key values and removes the mapping for a key from this map if it is present. For example:
Input:
Output:
Traversal of a Hashtable:
An advanced “for loop” is used in Hashtable in java to iterate the table. For example:
Input:
Output:
Output:
Output:
Output:
Output:
Output:
Output:
Read our Popular Articles related to Software
Points to Remember- Hashtable in Java
- It contains unique elements.
- It contains values based on the key.
- It is an array of a list, and each list is known as a bucket.
- The hashcode() method is called to identify the position of the bucket.
- The default capacity of Hashtable in java is 11 initially.
- The loadFactor of the Hashtable class is 0.75.
- Hashtable in java is synchronized.
- Only non-null value can be used as a key or value.
- It doesn’t guarantee any order.
- The enumerator in Hashtable in java is not fail-fast.
Amid increased conversations around crypto and Blockchain technology, if you wish to educate yourself professionally on the topic, then upGrad’s Executive Post Graduate Programme in Software Development – Specialisation in Blockchain under IIIT- Bangalore is the right choice for you!
RELATED PROGRAMS