The Wayback Machine - https://web.archive.org/web/20240906181527/https://www.geeksforgeeks.org/hashtable-clone-method-in-java/
Open In App

Hashtable clone() Method in Java

Last Updated : 28 Jun, 2018
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The java.util.Hashtable.clone() method is used to return a shallow copy of the mentioned hash table. It just creates a copy of the table.

Syntax:

Hash_Table.clone()

Parameters: The method does not take any parameters.

Return Value: The method just returns a copy of the Hashtable.

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





Output:

The Hashtable is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
The cloned table look like this: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}

Program 2:





Output:

The Hashtable is: {You=30, Welcomes=25, 4=15, Geeks=20}
The cloned table look like this: {You=30, Welcomes=25, 4=15, Geeks=20}

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


Previous Article
Next Article

Similar Reads

Hashtable toString() Method in Java
The toString() method in the Hashtable class in Java returns a string representation of the key-value mappings contained in the hashtable. The string is in the form of a comma-separated list of key-value pairs enclosed in curly braces. The toString() method is inherited from the java.util.Hashtable class's parent class, java.util.Dictionary. Here's
2 min read
Hashtable elements() Method in Java
The elements() method in the Hashtable class in Java returns an enumeration of the values contained in the hashtable. The enumeration returned by this method traverses the hashtable's values in the order in which they were added to the hashtable. The elements() method is inherited from the java.util.Hashtable class's parent class, java.util.Diction
2 min read
Hashtable keys() Method in Java
As we all know enumeration defines java class type so do enumerations can have constructors, methods, and instance variables. The java.util.Hashtable.keys() method of Hashtable class in Java is used to get the enumeration of the keys present in the hashtable. Illustration: Syntax: public Enumeration<K> keys() Enumeration enu = Hash_table.keys
2 min read
Hashtable containsValue() Method in Java
The java.util.Hashtable.containsValue() method is used to check whether a particular value is being mapped by a single or more than one key in the Hashtable. It takes the Value as a parameter and returns True if that value is mapped by any of the keys in the table. Syntax: Hash_Table.containsValue(Object Value) Parameters: The method takes just one
2 min read
Hashtable containsKey() Method in Java
The java.util.Hashtable.containsKey() method is used to check whether a particular key is present in the Hashtable or not. It takes the key element as a parameter and returns True if that element is present in the table. Syntax: Hash_table.containsKey(key_element) Parameters: The method takes just one parameter key_element which refers to the key w
2 min read
Hashtable put() Method in Java
The java.util.Hashtable.put() method of Hashtable is used to insert a mapping into a table. This means we can insert a specific key and the value it is mapping to into a particular table. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole. Syntax: Hash
3 min read
Hashtable get() Method in Java
The java.util.Hashtable.get() method of Hashtable class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the table contains no such mapping for the key. Syntax: Hash_Table.get(Object key_element) Parameter: The method takes one parameter key_element of object type and refers to the k
2 min read
Hashtable isEmpty() Method in Java
The java.util.Hashtable.isEmpty() method of Hashtable class is used to check for the emptiness of the table. The method returns True if no key-value pair or mapping is present in the table else False. Syntax: Hash_Table.isEmpty() Parameters: The method does not take any parameters. Return Value: The method returns boolean true if the table is empty
2 min read
Hashtable size() Method in Java
The java.util.Hashtable.size() method of Hashtable class is used to get the size of the table which refers to the number of the key-value pair or mappings in the Table. Syntax: Hash_Table.size() Parameters: The method does not take any parameters. Return Value: The method returns the size of the table which also means the number of key-value pairs
2 min read
Hashtable remove() Method in Java
The java.util.Hashtable.remove() is an inbuilt method of Hashtable class and is used to remove the mapping of any particular key from the table. It basically removes the values for any particular key in the Table.Syntax: Hash_Table.remove(Object key) Parameters: The method takes one parameter key whose mapping is to be removed from the Table.Return
2 min read
Hashtable clear() Method in Java
The java.util.Hashtable.clear() method in Java is used to clear and remove all of the keys from a specified Hashtable. Syntax: Hash_table.clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate the working of java.util.Hashtable.clear() Method: Program
2 min read
Hashtable contains() Method in Java
The java.util.Hashtable.contains(Object value) method in Java is used to check whether a particular value is being mapped by any keys present in the Hashtable. Syntax: Hash_table.contains(Object value) Parameters: The method accepts one parameter value of object type and refers to the value of the hashtable whose mapping is to be verified. Return V
2 min read
HashTable compute() method in Java with Examples
The compute(Key, BiFunction) method of Hashtable class allows to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping is found). If the remapping function passed in compute() of Hashtable returns null as a return value then the mapping is removed from Hashtable(or remains absent if initially a
3 min read
Hashtable computeIfAbsent() method in Java with Examples
The computeIfAbsent(Key, Function) method of Hashtable class which allows you to compute value of a mapping for specified key if key is not already associated with a value (or is mapped to null). If mapping function of this method returns null, then no mapping is recorded. If the remapping function throws an exception, the exception is rethrown, an
2 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: This method accepts two parameters: key: specifies
2 min read
HashTable forEach() method in Java with Examples
The forEach(BiConsumer) method of Hashtable class perform the BiConsumer operation on each entry of hashtable until all entries have been processed or the action throws an exception. The BiConsumer operation is a function operation of key-value pair of hashtable performed in the order of iteration. Method traverses each element of Hashtable until a
2 min read
Hashtable keySet() Method in Java with Examples
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 return
2 min read
How HashTable Works Internally in Java?
Hashtable is a kind of Hash map but is synchronized. Hash map is non–synchronized, permits one null key & multiple null values, not-thread safe i.e. cannot share between many threads without proper synchronization, the key/values pairs are stored in Hashtable. The Hash table does not allow null value/key. When using this you specify an object t
11 min read
Difference Between Hashtable and Synchronized Map in Java
The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a Hashtable, the objects used as keys must implement the hashCode method and also the equals method. Features of Hashtable: It is kind of like HashMap but is synchronized.Has
3 min read
Difference Between ConcurrentHashMap, HashTable and Synchronized Map in Java
Here we will be discussing out one by one individually then rolling onto the difference after having an adequate understanding of all three of them. We will be discussing ConcurrentHashMap first followed by HashTable, lastly followed by Synchronized Map. Let us start with ConcurrentHashMap first. The underlying data structure for ConcurrentHashMap
6 min read
Differences between HashMap and HashTable in Java
HashMap and Hashtable store key and value pairs in a hash table. When using a Hashtable or HashMap, we specify an object that is used as a key and the value that you want to be linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table. Now let us discuss with the help
3 min read
Hashtable in Java
Let us start with a simple Java code snippet that demonstrates how to create and use a HashTable. [GFGTABS] Java import java.util.Hashtable; public class HashtableCreation { public static void main(String args[]) { // Create a Hashtable of String keys and Integer values Hashtable<String, Integer> hashtable = new Hashtable<>(); // Adding
14 min read
LinkedList clone() Method in Java
The Java.util.LinkedList.clone() method is used to create a shallow copy of the mentioned linked list. It just creates a copy of the list. Syntax: LinkedList.clone() Parameters: This method does not take any parameters. Return Value: This function returns a copy of the instance of Linked list. Below program illustrate the Java.util.LinkedList.clone
1 min read
HashSet clone() Method in Java
The Java.util.HashSet.clone() method is used to return a shallow copy of the mentioned hash set. It just creates a copy of the set. Syntax: Hash_Set.clone() Parameters: The method does not take any parameters. Return Value: The method just returns a copy of the HashSet. Below program illustrate the Java.util.HashSet.clone() method: // Java code to
1 min read
TreeSet clone() Method in Java
The Java.util.TreeSet.clone() method is used to return a shallow copy of the mentioned tree set. It just creates a copy of the set. Syntax: Tree_Set.clone() Parameters: The method does not take any parameters. Return Value: The function just returns a copy of the TreeSet. Below program illustrates the use of Java.util.TreeSet.clone() method: // Jav
1 min read
HashMap clone() Method in Java
Let us do clear out the foundation concept of shallow copy and deep copy in java. Shallow repetition is quicker. However, it’s “lazy” it handles pointers and references. Rather than creating a contemporary copy of the particular knowledge the pointer points to, it simply copies over the pointer price. So, each the first and therefore the copy can h
3 min read
TreeMap clone() Method in Java with Examples
In Java, clone() method of the TreeMap class is used to return a shallow copy of the mentioned treemap. It just creates a copy of the map. --> java.util Package --> TreeMap Class --> clone() Method Syntax: Tree_Map.clone() Parameters: The method does not take any parameters. Return Type: A copy of the TreeMap. Example 1: Mapping string val
2 min read
EnumSet clone() Method in Java
The Java.util.EnumSet.clone() method in Java is used to return a shallow copy of the existing or this set. Syntax: Enum_Set_2 = Enum_Set_1.clone() Parameters: The method does not take any parameters. Return Value: The method does not return any value. Below programs illustrate the working of Java.util.EnumSet.clone() method: Program 1: // Java prog
2 min read
EnumMap clone() Method in Java
The Java.util.EnumMap.clone() method in Java is used to copy the mapped values of one map to another. It basically creates a shallow copy of this map. Syntax: Enum_map_2 = Enum_map_1.clone() Parameters: The method does not accept any argument. Return Value: The method returns a shallow copy of a EnumMap. Below programs illustrate the Java.util.Enum
2 min read
ArrayDeque clone() Method in Java
The Java.util.ArrayDeque.clone() method is used to return a shallow copy of this deque. It just creates a copy of the deque. Syntax: Array_Deque.clone() Parameters: The method does not take any parameter. Return Value: The method just returns a copy of the ArrayDeque. Below programs illustrate the Java.util.ArrayDeque.clone() method: Program 1: //
2 min read