Skip to content
- Tutorials
- Python Tutorial
- Taking Input in Python
- Python Operators
- Python Data Types
- Python Loops and Control Flow
- Python Functions
- Python OOPS Concept
- Python Data Structures
- Python Exception Handling
- Python File Handling
- Python Exercises
- Java
- Java Programming Language
- Java Collections
- Java 8 Tutorial
- Java Programs
- Java Interview Questions
- Java Exercises
- Java Quiz
- Java Projects
- Advance Java
- Programming Languages
- System Design
- Interview Corner
- Computer Science Subjects
- DevOps
- Linux
- Software Testing
- Databases
- Android
- Excel
- Mathematics
- Commerce
- Aptitude
- Python Tutorial
- DSA
- Data Structures
- Algorithms
- Analysis of Algorithms
- Searching Algorithms
- Sorting Algorithms
- Greedy Algorithms
- Dynamic Programming
- Graph Algorithms
- Pattern Searching
- Recursion
- Backtracking
- Divide and Conquer
- Mathematical Algorithms
- Geometric Algorithms
- Bitwise Algorithms
- Randomized Algorithms
- Branch and Bound
- Algorithms Tutorial
- DSA Tutorial
- Practice
- All DSA Problems
- Problem of the Day
- Company Wise Coding Practice
- GfG SDE Sheet
- Practice Problems Difficulty Wise
- Language Wise Coding Practice
- Curated DSA Lists
- Competitive Programming
- Company Wise SDE Sheets
- DSA Cheat Sheets
- Top Interview Questions
- Puzzles
- Data Science
- Web Tech
- Courses
-
- C# | Adding an element into the Hashtable
- C# | Adding an element to the List
- C# | Add element to HashSet
- C# | Copying the Hashtable elements to an Array Instance
- C# | Remove all elements from the Hashtable
- C# | Adding elements to the end of the ArrayList
- C# | Add element to SortedSet
- C# | Adding the elements to the end of the ArrayList
- C# | How to insert an element in an Array?
- C# Hashtable with Examples
- C# | Remove all elements from a HashSet
- C# | Number of elements in HashSet
- C# | Remove the specified element from a HashSet
- Hashtable elements() Method in Java
- How to Add or Update Elements in a HashTable in Java?
- Hashtable containsValue() Method in Java
- Hashtable get() Method in Java
- How to Use Enumeration to Display Elements of Hashtable in Java?
- Java Program to Implement Hash Table Chaining with List Heads
- Program to implement Hash Table using Open Addressing
Last Updated : 01 Feb, 2019
Improve
Improve
Like Article
Like
Save
Report
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Add(Object, Object) Method is used to adds an element with the specified key and value into the Hashtable.
Syntax:
public virtual void Add(object key, object value);
Parameters:
key: It is the specified key of the element to add of type System.Object.
value: It is the specified value of the element to add of type System.Object. The value can be null.
Exceptions:
- ArgumentNullException : If the key is null.
- ArgumentException : If an element with the same key already exists in the Hashtable.
- NotSupportedException : Either Hashtable is read-only or Hashtable has a fixed size.
Below given are some examples to understand the implementation in a better way :
See AlsoEverything you wanted to know about hashtables - PowerShellMicrosoft Exchange Server Powershell Cookbook (Update)How to add/remove values in the array in PowerShell?Example 1 :
// C# code for adding an element with the
// specified key and value into the Hashtable
using
System;
using
System.Collections;
class
GFG {
// Driver code
public
static
void
Main()
{
// Creating a Hashtable
Hashtable myTable =
new
Hashtable();
// Adding elements in Hashtable
myTable.Add(
"g"
,
"geeks"
);
myTable.Add(
"c"
,
"c++"
);
myTable.Add(
"d"
,
"data structures"
);
myTable.Add(
"q"
,
"quiz"
);
// Get a collection of the keys.
ICollection c = myTable.Keys;
// Displaying the contents
foreach
(
string
str
in
c)
Console.WriteLine(str +
": "
+ myTable[str]);
}
}
Output:
d: data structuresc: c++q: quizg: geeks
Example 2:
// C# code for adding an element with the
// specified key and value into the Hashtable
using
System;
using
System.Collections;
class
GFG {
// Driver code
public
static
void
Main()
{
// Creating a Hashtable
Hashtable myTable =
new
Hashtable();
// Adding elements in Hashtable
myTable.Add(
"4"
,
"Even"
);
myTable.Add(
"9"
,
"Odd"
);
myTable.Add(
"5"
,
"Odd and Prime"
);
myTable.Add(
"2"
,
"Even and Prime"
);
// Get a collection of the keys.
ICollection c = myTable.Keys;
// Displaying the contents
foreach
(
string
str
in
c)
Console.WriteLine(str +
": "
+ myTable[str]);
}
}
Output:
5: Odd and Prime9: Odd2: Even and Prime4: Even
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.collections.hashtable.add?view=netframework-4.7.2
Improve
Please Login to comment...
Similar Reads
C# | Check if a Hashtable is equal to another Hashtable
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Syntax: myTable1.Equals(myTable2) Here, myTable1 and myTable2 are the two Hashtables which is to be checked. Below given are some examples to understand the implementation in
2 min read
C# | Remove the element with the specified key from the Hashtable
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Remove(Object) Method is used to remove the element with the specified key from the Hashtable. Syntax: public virtual void Remove (object key); Parameter: key: It i
2 min read
C# | Check if Hashtable has a fixed size
Hashtable.IsFixedSize Property is used to get a value which indicates whether the Hashtable has a fixed size or not. Syntax: public virtual bool IsFixedSize { get; } Return Value: This property returns true if the Hashtable has a fixed size otherwise it returns false. The default is false. Below programs illustrate the above-discussed property: Exa
2 min read
C# | Check if the Hashtable contains a specific Key
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsKey(Object) Method is used to check whether the Hashtable contains a specific key or not. Syntax: public virtual bool ContainsKey(object key); Parameter: ke
2 min read
C# | Check if the Hashtable contains a specific Value
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsValue(Object) Method is used to check whether the Hashtable contains a specific value or not. Syntax: public virtual bool ContainsValue(object value); Param
2 min read
C# | Remove all elements from the Hashtable
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Clear Method is used to remove all elements from the Hashtable. Syntax: myTable.Clear() Here myTable is the name of the Hashtable. Exceptions: This method will give
2 min read
C# | Count the number of key/value pairs in the Hashtable
The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Count Property is used to get the total number of the key/value pairs contained in the Hashtable. Syntax: myTable.Count Here, myTable is the name of the Hashtable.
2 min read
C# | Get or Set the value associated with specified key in Hashtable
Hashtable.Item[Object] Property is used to get or set the value associated with the specified key in the Hashtable. Syntax: public virtual object this[object key] { get; set; } Here, key is key of object type whose value is to get or set. Exceptions: ArgumentNullException: If the key is null. NotSupportedException: If the property is set and the Ha
3 min read
C# | Gets an ICollection containing the keys in the Hashtable
Hashtable.Keys Property is used to get an ICollection containing the keys in the Hashtable. Syntax: public virtual System.Collections.ICollection Keys { get; } Return Value: This property returns an ICollection containing the keys in the Hashtable. Note: The order of keys in the ICollection is unspecified. Retrieving the value of this property is a
2 min read
C# | Gets an ICollection containing the values in the Hashtable
Hashtable.Values Property is used to get an ICollection containing the values in the Hashtable. Syntax: public virtual System.Collections.ICollection Values { get; } Return Value: This property returns an ICollection containing the values in the Hashtable. Note: The order of values in the ICollection is unspecified. Retrieving the value of this pro
2 min read
Article Tags :
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
'); $('.spinner-loading-overlay').show(); jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id, check: true }), success:function(result) { jQuery.ajax({ url: writeApiUrl + 'suggestions/auth/' + `${post_id}/`, type: "GET", dataType: 'json', xhrFields: { withCredentials: true }, success: function (result) { $('.spinner-loading-overlay:eq(0)').remove(); var commentArray = result; if(commentArray === null || commentArray.length === 0) { // when no reason is availaible then user will redirected directly make the improvment. // call to api create-improvement-post $('body').append('
'); $('.spinner-loading-overlay').show(); jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id, }), success:function(result) { $('.spinner-loading-overlay:eq(0)').remove(); $('.improve-modal--overlay').hide(); $('.unlocked-status--improve-modal-content').css("display","none"); $('.create-improvement-redirection-to-write').attr('href',writeUrl + 'improve-post/' + `${result.id}` + '/', '_blank'); $('.create-improvement-redirection-to-write')[0].click(); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); return; } var improvement_reason_html = ""; for(var comment of commentArray) { // loop creating improvement reason list markup var comment_id = comment['id']; var comment_text = comment['suggestion']; improvement_reason_html += `
${comment_text}
`; } $('.improvement-reasons_wrapper').html(improvement_reason_html); $('.improvement-bottom-btn').html("Create Improvement"); $('.improve-modal--improvement').hide(); $('.improvement-reason-modal').show(); }, error: function(e){ $('.spinner-loading-overlay:eq(0)').remove(); // stop loader when ajax failed; }, }); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); }); $('.left-arrow-icon_wrapper').on('click',function(){ if($('.improve-modal--suggestion').is(":visible")) $('.improve-modal--suggestion').hide(); else{ $('.improvement-reason-modal').hide(); } $('.improve-modal--improvement').show(); }); jQuery('.suggest-bottom-btn').on('click', function(){ var suggest_val = $.trim($("#suggestion-section-textarea").val()); var error_msg = false; if(suggest_val != ""){ if(suggest_val.length <= 2000){ jQuery('body').append('
'); jQuery('.spinner-loading-overlay').show(); jQuery.ajax({ type:'post', url: "https://apiwrite.geeksforgeeks.org/suggestions/auth/create/", xhrFields: { withCredentials: true }, crossDomain: true, contentType:'application/json', data: JSON.stringify({ "gfg_post_id" : `${post_id}`, "suggestion" : `
${suggest_val}
` }), success:function(data) { jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-section-textarea').val(""); jQuery('.suggest-bottom-btn').html("Sent "); setTimeout(() => { jQuery('.improve-modal--overlay').hide(); $('.improve-modal--suggestion').hide(); }, 1000); }, error:function(data) { jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-modal-alert').html("Something went wrong."); jQuery('#suggestion-modal-alert').show(); error_msg = true; } }); } else{ jQuery('#suggestion-modal-alert').html("Character limit exceeded."); jQuery('#suggestion-modal-alert').show(); jQuery('#suggestion-section-textarea').focus(); error_msg = true; } } else{ jQuery('#suggestion-modal-alert').html("Enter valid input."); jQuery('#suggestion-modal-alert').show(); jQuery('#suggestion-section-textarea').focus(); error_msg = true; } if(error_msg){ setTimeout(() => { jQuery('#suggestion-section-textarea').focus(); jQuery('#suggestion-modal-alert').hide(); }, 3000); } }) $('.improvement-bottom-btn.create-improvement-btn').click(function() { //create improvement button is clicked $('body').append('
'); $('.spinner-loading-overlay').show(); // send this option via create-improvement-post api jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id }), success:function(result) { $('.spinner-loading-overlay:eq(0)').remove(); $('.improve-modal--overlay').hide(); $('.improvement-reason-modal').hide(); $('.create-improvement-redirection-to-write').attr('href',writeUrl + 'improve-post/' + `${result.id}` + '/', '_blank'); $('.create-improvement-redirection-to-write')[0].click(); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); });